---
title: "How do I load a .env file in Node.js without dotenv?"
description: "Node.js can read .env files on its own. Use the --env-file flag when starting your app, or call process.loadEnvFile() in code, and drop the dotenv dependency."
url: https://www.danny.engineering/answer/nodejs/how-do-i-load-a-env-file-in-nodejs-without-dotenv
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Node.js"]
---

# How do I load a .env file in Node.js without dotenv?

## The short answer


Node.js has built-in support for `.env` files, so you no longer need the `dotenv` package for the common case. Pass `--env-file` when you start the process, and the variables appear on `process.env` before your code runs.


```bash
node --env-file=.env server.js
```


## Why it happens


For a long time Node.js only read environment variables from the shell that started it. Loading a `.env` file meant installing `dotenv` and calling it as early as possible, before any module that read `process.env`.


Node.js added the `--env-file` flag in v20.6.0, and later a programmatic `process.loadEnvFile()` and a `util.parseEnv()` helper. Env file support is stable in the current docs, and because Node.js 20 has reached end of life, every supported release line has it. If you are on Node.js 22 or 26, you can remove `dotenv` from most projects.


The file format is the familiar one: one `KEY=value` per line, with `#` for comments and quotes for values that contain spaces.


```bash
# .env
DATABASE_URL=postgres://localhost:5432/app
API_KEY=dev-key-123
GREETING="Hello there"
```


## How to fix it


### Use the --env-file flag


This is the recommended option. The file is read before any of your code runs, so there are no ordering problems with imports:


```bash
node --env-file=.env server.js
```


Put it in your `package.json` scripts so nobody has to remember it:


```json
{
  "scripts": {
    "dev": "node --watch --env-file=.env src/server.js",
    "start": "node src/server.js"
  }
}
```


You can pass the flag more than once. Files are read in order and later files override earlier ones, which suits a shared file plus local overrides:


```bash
node --env-file=.env --env-file=.env.local server.js
```


Variables that are already set in the real environment win over anything in the files. That is usually what you want: a value set by your hosting platform or CI is not replaced by a stray local file.


### Make the file optional with --env-file-if-exists


`--env-file` throws an error if the file is missing. That is helpful locally but awkward in production, where values normally come from the platform. Use `--env-file-if-exists` to load the file when it is there and carry on quietly when it is not:


```bash
node --env-file-if-exists=.env server.js
```


### Call process.loadEnvFile() in code


When you cannot change the start command, for example inside a script run by another tool, load the file from code. With no argument it reads `.env` in the current working directory:


```javascript
process.loadEnvFile();

// or a specific file
process.loadEnvFile('./config/.env.test');

console.log(process.env.DATABASE_URL);
```


Call it before you import anything that reads `process.env` at the top level. In ES modules, static imports are hoisted and run first, so put the call in a small entry file and load the rest of the app with a dynamic `import()` afterwards:


```javascript
// entry.js
process.loadEnvFile();
await import('./server.js');
```


Like the flag, `process.loadEnvFile()` does not overwrite variables that already exist in `process.env`.


### Parse without touching process.env


If you want the values as a plain object, for validation or to pass to a config function, read the file and use `util.parseEnv()`:


```javascript
import { readFile } from 'node:fs/promises';
import { parseEnv } from 'node:util';

const contents = await readFile('.env', 'utf8');
const config = parseEnv(contents);

console.log(config.API_KEY);
```


This is also the way to make a file override existing variables on purpose, by assigning the parsed result to `process.env` yourself.


## Common pitfalls

- Expecting a file to override a variable already exported in your shell. Both the flag and `process.loadEnvFile()` leave existing values alone, so check `echo $API_KEY` if a value seems stuck.
- Calling `process.loadEnvFile()` after a module has already read its config. Static `import` statements run before the rest of the file, so the module sees `undefined`.
- Putting `--env-file` after the script name. Node.js options must come before the file, otherwise they are passed to your script as arguments: use `node --env-file=.env app.js`, not `node app.js --env-file=.env`.
- Relying on variable expansion or multi-environment features from `dotenv` extensions. The built-in parser covers the standard format, so test anything unusual before removing the dependency.
- Committing `.env` to Git. Add it to `.gitignore` and commit an `.env.example` with placeholder values instead.

## Related reading

- [Node.js docs: --env-file](https://nodejs.org/api/cli.html#--env-filefile)
- [Node.js docs: process.loadEnvFile()](https://nodejs.org/api/process.html#processloadenvfilepath)
- [Node.js docs: util.parseEnv()](https://nodejs.org/api/util.html#utilparseenvcontent)
