-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fail for missing env file in development only #51451
Comments
cc @nodejs/tsc @GeoffreyBooth |
First, we’ve been discouraging use of Second, if you’re running There’s an assumption implicit in the problem as posed: that the same command needs to be used in both development and CI. Why is that the case? It’s common to run different commands in CI, for example |
I think the reason people are asking for this is that it is the behavior of most libraries that support .env (to ignore a missing .env) since the typical flow is to not check in .env files to source control. I'm not saying it's good behavior or we should change to it (and I approved the "fail on it" PR) - just explaining why it's the expectation. |
I'm not ok in adding any support for I'm also ok in always ignoring errors if there is no .env file to load, and print a warning instead. |
I'm also ok with a warning |
Is it possible to add some options, specifically to the programmatic API, to quietly handle missing files ? This could be useful for other things down the line too |
The programmatic API has a slightly different purpose; by the time your code is running, it’s impossible to set It’s very easy to put a try/catch around a call in JavaScript, so I’m not sure it’s worth the complexity of adding an option for ignoring missing files; though if we do, it should be I still think the solution there isn’t for Node to stop erroring on missing files, but rather for the user to use other logic outside of Node to know whether or not |
There is an easy non-breaking-change solution: A new Node.js arg Edit: I now realize there is a feature request issue for exactly that: |
Fixes: nodejs#50993 Refs: nodejs#51451
This has been superceded by #50993 |
Fixes: nodejs#50993 Refs: nodejs#51451
Fixes: nodejs#50993 Refs: nodejs#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
Fixes: nodejs#50993 Refs: nodejs#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
Fixes: nodejs#50993 Refs: nodejs#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
Fixes: nodejs#50993 Refs: nodejs#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
Fixes: nodejs#50993 Refs: nodejs#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
Fixes: nodejs#50993 Refs: nodejs#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
Fixes: nodejs#50993 Refs: nodejs#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
Fixes: #50993 Refs: #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: #53060 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Fixes: #50993 Refs: #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: #53060 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Fixes: #50993 Refs: #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: #53060 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Fixes: #50993 Refs: #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: #53060 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Fixes: #50993 Refs: #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: #53060 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Fixes: nodejs#50993 Refs: nodejs#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: nodejs#53060 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
What is the problem this feature will solve?
50588 changed behavior of the
--env-file
option so Node will exit with an error if the env file does not exist. This change was motivated by 50536.This change breaks npm scripts in CI, where it's normal to have no env file. For example, a
test
script.What is the feature you are proposing to solve the problem?
Option 1a: Do not exit with an error unless
NODE_ENV=development
.Option 1b: Do not exit with an error if
NODE_ENV
is notdevelopment
.Option 2: Instead of exiting with an error, warn and continue. In 50536, OP suggested an error or a warning, though later said an error would be consistent with
node:fs
behavior.Option 3: Revert 50588.
What alternatives have you considered?
Workaround 1: In the CI script, add
touch .env
or similar to create an empty file. This workaround feels kludgey. (Creating env files in CI. Empty files just to get around a requirement that only consider the development perspective.) It also wouldn't work for Windows CI environments.Workaround 2: Copy npm scripts that use
--env-file
and make:ci
versions of them for CI without--env-file
. This workaround requires duplicate npm scripts that must be kept in sync, which creates an opportunity for divergence between development and CI that can lead to unexpected errors.Workaround 3: Check the env file into the repo. I hope I don't need to explain why this is a bad idea.
Discussion
50536 and 50588 arguments
50536 gives an example of a monorepo and changing context of the current directory or lacking permissions to read the env file. I may be misreading, but this example sounds like an issue specific to an individual's development environment that would not apply in a CI environment.
50588 asserts that "the env file is a crucial file." This assertion is usually true an individual developer's personal development environment, but false in other environments.
Counterargument
Non-development environments (CI, test/staging, production, etc.) usually get environment variables from the environment, not a file. Applications cannot rely on Node to fail them if an env file is missing in these environments.
Therefore, applications should confirm required environment values are set and have valid values and fail or provide reasonable defaults if they are not. For example,
if process.env.REQUIRED_ENV_VARIABLE === undefined { process.exit(1); }
orif (Object.hasOwn(process.env, 'REQUIRED_ENV_VARIABLE')) { process.exit(1); }
.Given the monorepo example in 50536, one would suspect the project is non-trivial. The missing permissions point implies the developer can't set ACLs, which sounds like a large organization with robust risk controls. In neither case should the application assume environment variables are set.
Comparing options 1a and 1b
Options 1a and 1b seek to strike a balance between the needs of development and CI by making this behavior dependent on
NODE_ENV
, but differ in their approach.Option 1a requires setting
NODE_ENV
in development to get a failure if the env file doesn't exist.Option 1b requires setting
NODE_ENV
in other environments. For example, I'd need to addNODE_ENV=ci
(or any other value that isn'tdevelopment
) in my CI environment configuration. It's a little extra work, but doesn't feel kludgey like workaround 1 or add npm scripts like workaround 2.Option 1b is most consistent with existing patterns. "Node.js assumes it's always running in a development environment." ref. Many packages change logging and other behaviors based on the value of
NODE_ENV
. (Let's avoid the "toNODE_ENV
or not toNODE_ENV
" debate, please.)If 1a and 1b are unacceptable, then options 2 and 3 let us don't need kludgey workarounds or duplicate scripts to make CI work.
The text was updated successfully, but these errors were encountered: