node.js 在 npm-scripts 中传递环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37140799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing environment variables in npm-scripts
提问by Hans
I have a package.json with following (simplified) content in the scripts key:
我有一个 package.json 在脚本键中包含以下(简化的)内容:
...
scripts: {
"start": "NODE_ENV=${NODE_ENV:=production} node start-app.js",
"poststart": "echo $NODE_ENV"
}
...
From the command line I can run:
从命令行我可以运行:
npm start
This will run my start-app.js script and set the process.env.NODE_ENV environment variable to "production". See herefor syntax explanation.
这将运行我的 start-app.js 脚本并将 process.env.NODE_ENV 环境变量设置为“生产”。有关语法说明,请参见此处。
The poststart will automatically run after start as described here.
如上所述启动后会自动开始运行后这里。
However poststart will not "inherit" the NODE_ENV shell environment variable, so the echo command will not echo anything.
但是 poststart 不会“继承” NODE_ENV shell 环境变量,因此 echo 命令不会回显任何内容。
My producation code is a little more complex, but what I am trying to accomplish is passing down the NODE_ENV variable from the "starting point" to dependent scripts. Any suggestions/best practices on how to do that?
我的生产代码有点复杂,但我想要完成的是将 NODE_ENV 变量从“起点”传递给相关脚本。关于如何做到这一点的任何建议/最佳实践?
I dont want to hardcode the NODE_ENV in the poststart, because I might want to do:
我不想在 poststart 中对 NODE_ENV 进行硬编码,因为我可能想要这样做:
NODE_ENV=development npm start
and I want everyting "down the chain" inherit the same environment.
我希望每一个“链下”都继承相同的环境。
回答by Mario Tacke
You have a few options:
您有几个选择:
- better-npm-run,which can define an
envfor each command separately - Instead of a
poststartscript, you can concatenate commands for npm like so:"start": "NODE_ENV=${NODE_ENV:=production} node start-app.js && echo $NODE_ENV" - Use a process manager in production like pm2. pm2 lets you define environment specific json files with settings such as
NODE_ENV. At our company, we successfully run all of our apps in different environments with pm2 (all the while having the same start command)
- Better-npm-run,它可以
env为每个命令分别定义一个 poststart您可以像这样为 npm 连接命令,而不是脚本:"start": "NODE_ENV=${NODE_ENV:=production} node start-app.js && echo $NODE_ENV"- 在生产中使用流程管理器,如pm2。pm2 允许您使用 .json 等设置定义环境特定的 json 文件
NODE_ENV。在我们公司,我们使用 pm2 在不同环境中成功运行了所有应用程序(同时使用相同的启动命令)
回答by tylim
this is how I did it, first you need to install two dev-dependencies
我就是这样做的,首先你需要安装两个开发依赖项
https://www.npmjs.com/package/env-cmdthis load your env var from your file
https://www.npmjs.com/package/env-cmd这会从您的文件中加载您的 env var
https://www.npmjs.com/package/cross-envthis use environment variable in script
https://www.npmjs.com/package/cross-env这在脚本中使用环境变量
example scripts:
示例脚本:
"env-cmd ./.config/prod.env cross-env-shell \"docker volume create $DOCKER_VOLUME\""
this load $DOCKER_VOLUME env var from prod.env
这从 prod.env 加载 $DOCKER_VOLUME env var
update: starting from env-cmd version 10, you need specify -f flag if you want to use a custom env file path
更新:从 env-cmd 版本 10 开始,如果要使用自定义 env 文件路径,则需要指定 -f 标志
"env-cmd -f ./.config/prod.env cross-env-shell \"docker volume create $DOCKER_VOLUME\""
回答by xdeepakv
If you have small use cased, use better-npm-run. For small cases, it works fine. Somehow if you have a lot of commands and it hard manage. Try, batman-cli. Work well and handle lot of environment-dependent issues
如果您的用例很小,请使用 better-npm-run。对于小案例,它工作正常。不知何故,如果您有很多命令并且很难管理。试试吧,batman-cli。工作良好并处理许多与环境相关的问题
npm i -g batman-cli
npm i -g batman-cli

