---
title: "Loading env file into Node process"
description: "Node.js 20 can load a .env file with the built-in --env-file flag, and --env-file-if-exists keeps the process from crashing when the file is missing."
canonical: https://omiid.me/notebook/40/loading-env-file-into-node-process
author: Omid Sayfun
published: 2025-02-06
updated: 2026-08-09
---

# Loading env file into Node process

Loading a Node.js env file no longer needs the [dotenv](https://www.npmjs.com/package/dotenv) package. Node.js 20 added an `--env-file` flag that reads a `.env` file into `process.env` before your code runs.

### Pass --env-file to load a .env file

The flag takes the path to the file and goes before the script name:

```shell
node --env-file .env main.js
```

Node reads the variables from that file into the process environment. No import, no dependency, no call at the top of your entry file.

The flag belongs to the `node` binary itself. If you start the app through a wrapper such as `ts-node` or `nodemon`, pass it to Node rather than to the wrapper.

### Use --env-file-if-exists when the file may be missing

If the `.env` file does not exist, `node --env-file` throws and the process exits. Use `--env-file-if-exists` instead when the file is optional:

```shell
node --env-file-if-exists=.env main.js
```

With `--env-file-if-exists`, a missing file is skipped and the process keeps running. That fits CI jobs and containers where the variables already come from the environment.

### Repeat the flag to load multiple env files

You can pass `--env-file` more than once, and later files override earlier ones:

```shell
node --env-file=.env --env-file=.dev.env main.js
```

This loads variables from both `.env` and `.dev.env`, so you can keep shared defaults in one file and per-environment overrides in another.

### tl;dr

Node.js 20 introduces the `--env-file` flag to load environment variables directly from a file, eliminating the need for external packages like dotenv. Use `--env-file-if-exists` to avoid errors if the file is missing, and you can load multiple files by specifying multiple `--env-file` flags.
