-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Add --env-file-if-exists
#50993
Comments
A few random thoughts: Another simple (yet non-portable) alternative is to put something like this in your function env-file-if-exists() { if [ -f "$1" ]; then echo "--env-file=$1"; fi } And then use it like this: node $(env-file-if-exists local.env) Or, for better readability, something like this: function if-exists() { if [ -f "$1" ]; then echo "$1"; else echo /dev/null; fi } To be used as: node --env-file=$(if-exists local.env)
On a side note, from a security point of view, I think this is an anti-pattern. Loading environment variables from a file that may or may not exist opens up a potential attack vector. |
this is easily achievable with a little shell statement: node $([[ -f .env ]] && echo "--env-file .env ")index.js |
This request is for a pure node solution |
I second that it would be really desirable to have this feature. We're working with a |
Also: - update GitHub actions to Node 20; - work around `.env` file not allowed to be optional since Node 20.11 (context and workaround in nodejs/node#50993).
Conundrum: use the "native" feature and add complexity to your own packages OR use the pre-existing npm package which is "not native" and requires an additional dependency tree. edit: original comment was maybe rude |
I have a similar issue, posted here (and got 💩 on but ok). The recc. here would help me but Node is meant to be a generic runtime, not a batteries-included server package. insisting on installing zilch and instead asking it to do all the lifting is starting to sound a bit much. I have the same philosophy and don't install unless abs. necessary.
which list reasonable safe defaults, ie: NODE_ENV=development
DATABASE_URL=postgres://dev:123456@localhost:5432/app_dev
FOO=BAR All other Here's the runners: // remote envs dont load .env files, duh
"start": "node server/app.js",
"test-ci": "node --test test/**/**.spec.js",
// oh well
"start-dev": "node --env-file=.env.local.dev --watch server/app.js",
"test": "node --env-file=.env.local.test --test test/**/**.spec.js" internally we do something like this: const db = pg(DATABASE_URL) I think this kind of sucks, it's convoluted and has potential of someone seeing an A Github org. can setup server git hooks and reject anything resembling a cred. |
I support a Edit: Solved in #53060. Now we have to wait for it to be merged |
Fixes: nodejs#50993 Refs: nodejs#51451
I was setting up a GitHub action for my project and wanted to use secrets to set env variables in the YAML file depending on if it was a production or development build. In this situation, the |
That's exactly what we've been doing until now while waiting for a non-mandatory env injection version. All of our env-sensible scripts look something like that: |
The TSC had a vote on this, and IIRC this is the plan https://github.com/nodejs/TSC/blob/main/votes/2024-06-25-0.json |
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: 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>
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 introduced a throw if a requested env file isn't found. When dealing with multiple environments, a .env file may sometimes be merely optional - and throwing isn't desired. For example, during local development, devs might keep variables in a
.env
file, which isn't committed to source control, while in production, these variables come from the environment. This option will allow the env file to be loaded only if it exists without changing the command used to invoke node between environments.What is the feature you are proposing to solve the problem?
Add a flag (or other mechanism) to indicate that the .env file should be loaded only it's found, and to throw otherwise.
What alternatives have you considered?
touch
ing.env
before running node; invoking node via a shell script and "building up" the cli opts.The text was updated successfully, but these errors were encountered: