From c0033708dc292d66f1e1d09a795060f770652918 Mon Sep 17 00:00:00 2001 From: Bosco Domingo <46006784+BoscoDomingo@users.noreply.github.com> Date: Mon, 16 Sep 2024 02:58:16 +0200 Subject: [PATCH] src: add `--env-file-if-exists` 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 PR-URL: https://github.com/nodejs/node/pull/53060 Reviewed-By: Yagiz Nizipli Reviewed-By: Antoine du Hamel --- doc/api/cli.md | 16 ++++++++ src/node.cc | 18 ++++++--- src/node_dotenv.cc | 48 ++++++++++++++-------- src/node_dotenv.h | 6 ++- src/node_options.cc | 4 ++ src/node_options.h | 1 + test/parallel/test-dotenv-edge-cases.js | 54 ++++++++++++++++++++----- 7 files changed, 115 insertions(+), 32 deletions(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index a58ac7365eca31..cb7f68e0633fd4 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -818,6 +818,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 ``` @@ -857,6 +859,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`