如何在package.json中设置环境变量 [英] How to set environment variables from within package.json
问题描述
如何在package.json
内设置一些环境变量,以与npm start
类似的命令一起使用?
How to set some environment variables from within package.json
to be used with npm start
like commands?
这是我目前在package.json
中拥有的内容:
Here's what I currently have in my package.json
:
{
...
"scripts": {
"help": "tagove help",
"start": "tagove start"
}
...
}
我想在启动脚本中设置环境变量(例如NODE_ENV
),同时仍然能够仅使用一个命令npm start
来启动应用程序.
I want to set environment variables (like NODE_ENV
) in the start script while still being able to start the app with just one command, npm start
.
推荐答案
在脚本命令中设置环境变量:
Set the environment variable in the script command:
...
"scripts": {
"start": "node app.js",
"test": "env NODE_ENV=test mocha --reporter spec"
},
...
然后在您的应用中使用process.env.NODE_ENV
.
Then use process.env.NODE_ENV
in your app.
注意:env
确保它可以跨平台工作.如果只关心Mac/Linux,则可以忽略它.
Note: env
ensures that it works across platforms. You can omit it if you only care about Mac/Linux.
这篇关于如何在package.json中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!