Skip to content
All findings

dotenv 18: node -r dotenv/config app.js dotenv_config_path=.env.prod silently loads .env (or nothing): argv options ignored, no log, exit 0 — upgrade run 17.4.2 → 18.0.10

Published 4 Dec 2026

dotenv 18.0.0 shipped 2026-09-17, and 18.0.10 (2026-11-08) is latest, so npm i dotenv gets it. I ran the same commands against 17.4.2 and 18.0.10 on Node 26.11.1. run.sh installs both side by side and reproduces everything. Raw output is in output.txt.

What breaks silently

app.js prints DB_URL and MODE. .env has the dev values and .env.prod has the prod ones.

| # | command | 17.4.2 | 18.0.10 | |---|---|---|---| | 1 | node -r dotenv/config app.js dotenv_config_path=.env.prod | prod | dev (reads .env) | | 1b | same, but no .env in the directory | prod | everything unset, exit 0, no output | | 2 | DOTENV_CONFIG_PATH=.env.prod node -r dotenv/config app.js | prod | prod | | 3 | MODE=shell node -r dotenv/config app.js dotenv_config_override=true | MODE=dev (overridden) | `MODE=shell` (override ignored) | | 4 | MODE=shell DOTENV_CONFIG_OVERRIDE=true node -r dotenv/config app.js | MODE=dev | MODE=dev | | 5 | ... dotenv_config_debug=true (lines printed) | 3 | 1 (no debug output) |

Why: in 17, dotenv/config merged lib/env-options and lib/cli-options(process.argv). In 18 (dist/config.cjs) it is just:

js
const dotenv = require('./index.cjs')const quiet = process.env.DOTENV_QUIET != null ? process.env.DOTENV_QUIET : process.env.DOTENV_CONFIG_QUIETdotenv.config({ quiet: quiet != null ? quiet : true })

So process.argv is never read. Because preloads now default to quiet: true, nothing appears in the logs to show that the wrong file, or no file, was loaded. This pattern (node -r dotenv/config server.js dotenv_config_path=...) was the documented way to pick a file in 17, and it is common in package.json scripts, Dockerfile CMDs and PM2/systemd units. The process still starts. It just points at the dev database, or has no config at all.

Fix: switch to the env-var form (DOTENV_PATH=.env.prod node -r dotenv/config app.js, or the legacy DOTENV_CONFIG_PATH), or use 18's CLI (dotenv run -- node app.js with DOTENV_PATH). Then add a startup assertion for a required variable, because dotenv will never fail loudly here.

Documented changes that still surprise in an upgrade

These are documented in the 18 README ("DOTENV_* … provide defaults for config()" and "legacy DOTENV_CONFIG_* … still work as fallbacks"). They do change what existing code does:

  • DOTENV_PATH=.env.other node plain.js, where plain.js is an ordinary require('dotenv').config(): 17 ignores the variable and loads .env. 18 loads .env.other, and DB_URL becomes unset. The same happens with a leftover DOTENV_CONFIG_PATH, set for the preload in a Dockerfile: in 18 it also redirects every config() call.
  • MODE=shell DOTENV_OVERRIDE=1 node plain.js: 17 keeps the shell value (MODE=shell). 18 overwrites it with the file value (MODE=dev).
  • The "injected env" line moved from stdout to stderr, and the // tip: suffix is gone. Log scrapers keyed on stdout lose it.

What did *not* change: the parser

On a 28-key corpus (corpus.env.txt and crlf.env.txt: inline #, quotes, \n escapes, backticks, export, KEY: value, = in values, unterminated quotes, multiline values, CRLF, duplicates, dotted keys), the 17.4.2 parser, the 18 default parser and 18's opt-in { fast: true } parser gave identical results for all 28 keys.

Node's built-in util.parseEnv / node --env-file, the usual "drop dotenv" alternative, disagreed on 5 keys:

log
COLON           dotenv "colon-style"        node (absent)TRAILING_QUOTE  dotenv "\"abc\"extra"        node "abc"UNTERMINATED    dotenv "\"abc"               node "abc\nMULTI="   <- swallows the next lineMULTI           dotenv "first\nsecond"       node (absent)        <- lost because of the line aboveESCAPED_QUOTE   dotenv "say \\\"hi\\\""      node "say \\"        <- value truncated at the escaped quote

What the docs say

  • CHANGELOG 18.0.0: "Remove preloading. Instead use cli dotenv run -- your-command (#1035)". But -r dotenv/config and import 'dotenv/config' still load .env, and the 18 README still says "dotenv/config imports and preloads default to true" (for quiet). Neither the changelog nor the README mentions that the dotenv_config_* command-line arguments are gone. They're simply no longer documented, and I found no GitHub issue about it.
  • The env-var behaviour changes (DOTENV_PATH, DOTENV_OVERRIDE and the DOTENV_CONFIG_* fallbacks applying to config()) are documented in the README's options table.