Skip to content

Menu

dotenv Cheat Sheet: .env Syntax, Node --env-file and Compose

Checked against dotenv dotenv 18.0.10 (npm); Node 26.13.1 docs, examples run on Node 24.4.0; Docker Compose docs on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for anyone loading configuration from a .env file in Node or Docker Compose and wondering why a value came out different from what they typed. It covers the dotenv package (18.0.10 on npm today), Node's built-in --env-file from the 26.13.1 docs, and Compose's two separate uses of env files. The Node rows were run on Node 24.4.0; dotenv and Compose rows are from their docs because neither was available in this sandbox.

When to use what

There is no single .env standard. Each loader has its own parser, and they disagree on expansion, inline comments and escapes, so pick one loader per project and test the tricky values against it.

On Node 20.6 or later, start with the built-in node --env-file=.env app.js. The flag stopped being experimental in v24.10.0 and v22.21.0, needs no dependency and handles quotes, comments and multi-line values. Use process.loadEnvFile() when the path is decided at runtime. Reach for the dotenv package when you need its override option or the same behaviour on older Node versions. Neither one expands ${VAR}: in our run, EXPAND=${PORT}/api came through as the literal text. If you need expansion, dotenv's README points to dotenv-expand and dotenvx.

Docker Compose is the one people mix up most. The .env next to your compose file feeds interpolation of ${VAR} inside compose.yaml. The env_file: attribute injects variables into the container. They are separate features that happen to use the same file format. To scaffold a file, use the .env file generator; to produce a safe .env.example with values blanked, the template generator; and to catch duplicate keys or bad lines, the env validator. The secrets management guide covers what to do once you outgrow plain files.

Every row here was run with node --env-file=.env on Node 24.4.0 and the value printed with JSON.stringify.

PORT=3000

One KEY=value per line; the value always arrives as a string, "3000"

export MODE=dev

A leading export is ignored, so the same file can be sourced by a shell

INLINE=abc # trailing comment

Text after # on an unquoted value is dropped; the value was "abc"

HASH="abc # kept"

Quote the value to keep a # inside it

DOUBLE="line1\nline2"

Double quotes turn \n into a real newline

SINGLE='raw $PORT \n'

Single quotes keep everything literal: no newline, no expansion

MULTI="first second"

A quoted value may span lines (Node v20.12+ / v21.7+)

EXPAND=${PORT}/api

No variable expansion: the value stayed "${PORT}/api"

node --env-file=.env app.js

Load a file before your code runs; a missing file stops Node with "node: .env: not found"

node --env-file-if-exists=.env.local app.js

Same, but a missing file only prints a notice and Node carries on

node --env-file=.env --env-file=.env.local app.js

Later files override earlier ones; with PORT=1 then PORT=2 the app saw 2

PORT=9 node --env-file=.env app.js

A variable already set in the real environment wins over the file; the app saw 9

process.loadEnvFile(".env")

Load a file from code; the default path is ./.env and a missing file throws ENOENT

util.parseEnv("A=1\nB=\"x y\"")

Parse .env text into an object without touching process.env

From the dotenv README; not run here.

require("dotenv").config()

Read .env from the current directory into process.env, skipping keys that are already set

From the docs, not run by us
import "dotenv/config"

Load at import time in ES modules, before other imports read process.env

From the docs, not run by us
config({ path: [".env.local", ".env"] })

Load several files in order; the first value for a key wins unless override is set

From the docs, not run by us
config({ override: true })

Let file values replace variables that are already in the environment

From the docs, not run by us
config({ quiet: true })

Silence the startup log line that config() prints by default

From the docs, not run by us
dotenv.parse(buffer)

Parse .env content into a plain object without changing process.env

Try it: env-to-jsonFrom the docs, not run by us

Container precedence, highest first, per https://docs.docker.com/compose/how-tos/environment-variables/envvars-precedence/: docker compose run -e, an interpolated value, environment, env_file, then the image's ENV. Docker wasn't available here, so these are docs-only.

image: "app:${TAG:-latest}"

Interpolate from the shell or the project .env, falling back to latest when TAG is unset or empty

Try it: Docker Compose GeneratorFrom the docs, not run by us
${DB_URL:?DB_URL is required}

Fail the compose command with a message when the variable is unset or empty

From the docs, not run by us
env_file: [.env.common, .env.web]

Inject variables into the container; later files override earlier ones and paths are relative to compose.yaml

From the docs, not run by us
env_file: [{ path: .env.local, required: false }]

Skip the file silently if it is missing (Compose 2.24.0+)

From the docs, not run by us
environment: [DEBUG]

List a name with no value to pass it through from the shell running compose

From the docs, not run by us
docker compose --env-file .env.prod up

Use another file for interpolation instead of the project .env

From the docs, not run by us
docker compose config --environment

Show the variables Compose resolved for interpolation

From the docs, not run by us

Worked example: Find out which value Node actually loaded

A colleague's app reads PORT from .env, but on the CI runner it listens on the wrong port. Reproduce it with two files and a tiny script that prints the value (Node 24.4.0):

$ cat a.env
PORT=1
$ cat b.env
PORT=2
$ node --env-file=a.env --env-file=b.env show.js
PORT "2"
$ PORT=9 node --env-file=.env show.js
PORT "9"
$ node --env-file=nope.env show.js
node: nope.env: not found
$ node --env-file-if-exists=nope.env show.js
nope.env not found. Continuing without it.
PORT undefined

Three rules explain the bug. Later --env-file flags override earlier ones, so file order matters. A variable already set in the process environment beats every file, and CI runners often export PORT themselves, which is what happened here. Finally, --env-file treats a missing file as fatal, while --env-file-if-exists keeps going with the variable unset, so pick the flag that matches whether the file is optional.

Common mistakes

  • Committing a real .env file with secrets to the repository.

    Add .env to .gitignore, commit a .env.example with blank values, and rotate any secret that was ever pushed.

  • Expecting ${OTHER} inside a .env value to be expanded in Node.

    Neither dotenv nor --env-file expands variables; build the value in code or use dotenv-expand if you really need it.

  • Putting an unquoted value with a # in it, such as a colour or a password.

    Wrap the value in double or single quotes so the parser doesn't treat the rest of the line as a comment.

  • Assuming the project .env is passed into Compose containers automatically.

    That file only feeds ${VAR} interpolation in compose.yaml; add an env_file: or environment: entry to get values into the container.

  • Being surprised that a value in .env is ignored at runtime.

    Both dotenv and Node keep a variable that is already set in the environment; unset it, or use dotenv's override: true.