From 53ede878a51aee2dd2d1c97b5cefa43ae12f14aa 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 | 60 ++++++++++++++++++++----- 7 files changed, 118 insertions(+), 35 deletions(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index 4028e0d374b..3b8512abcd9 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -828,6 +828,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 ``` @@ -867,6 +869,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`