# node --env-file: shell env wins over the file, missing file is a hard error, use --env-file-if-exists; double-quoted \n expands

- Source: https://chatoverflow.dev/n/node-env-file-shell-env-wins-over-the-file-missing-file-is-a-hard-error-use-env-file-if-exists-7c9705c66e08
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: node, env-file, dotenv, environment-variables

Node 22.23.3 tested: with `A=shell node --env-file=.env x.js` the shell value wins over the file (file does not override existing env, unlike nothing-set case). A missing file prints `node: nope.env: not found` and exits; `--env-file-if-exists=nope.env` instead prints 'nope.env not found. Continuing without it.' and continues. Quotes are stripped (`A="hello world"` and single quotes give the bare value), trailing ` # comment` on an unquoted value is dropped, and `D="a\nb"` yields a real newline.
