From 84ffbaf15de02c26281710b81a703adbb3757f71 Mon Sep 17 00:00:00 2001 From: Bosco Domingo Date: Sun, 19 May 2024 20:22:24 +0200 Subject: [PATCH] src: add `--optional-env-file` flag Fixes: https://github.com/nodejs/node/issues/50993 Refs: https://github.com/nodejs/node/issues/51451 test: remove unnecessary comment src: conform to style guidelines src: change flag to `--env-file-optional` test: revert automatic linter changes doc: fix typos src: change flag to `--env-file-if-exists` src: refactor `env_file_data` and `GetEnvFileDataFromArgs` test: clean up tests src: print error when file not found test: remove unnecessary extras --- doc/api/cli.md | 16 ++++++++ src/node.cc | 18 ++++++--- src/node_dotenv.cc | 51 +++++++++++++++++-------- src/node_dotenv.h | 6 ++- src/node_options.cc | 4 ++ src/node_options.h | 1 + test/parallel/test-dotenv-edge-cases.js | 47 ++++++++++++++++------- 7 files changed, 108 insertions(+), 35 deletions(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index ffbc0718f8f076..64c6818a37fc97 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -824,6 +824,8 @@ in the file, the value from the environment takes precedence. You can pass multiple `--env-file` arguments. Subsequent files override pre-existing variables defined in previous files. +An error is thrown if the file does not exist. + ```bash node --env-file=.env --env-file=.development.env index.js ``` @@ -863,6 +865,9 @@ Export keyword before a key is ignored: export USERNAME="nodejs" # will result in `nodejs` as the value. ``` +If you want to load environment variables from a file that may not exist, you +can use the [`--env-file-if-exists`][] flag instead. + ### `-e`, `--eval "script"` + +Behavior is the same as [`--env-file`][], but an error is not thrown if the file +does not exist. + ### `--pending-deprecation`