用于express.js环境。有什么建议么?
如何在OS X中将NODE_ENV设置为生产/开发
Windows CMD -> set NODE_ENV=production
Windows Powershell -> $env:NODE_ENV="production"
MAC -> export NODE_ENV=production
If you are on windows. Open your cmd at right folder then first
set node_env={your env name here}
hit enter then you can start your node with
node app.js
it will start with your env setting
Daniel has a fantastic answer which is the better approach for the correct deployment (set and forget) process.
For those using express. You can use grunt-express-server which is fantastic as well. https://www.npmjs.org/package/grunt-express-server
对于Windows Powershell,请使用此命令
$env:NODE_ENV="production" ; node app.js
不必担心您是在Windows,Mac还是Linux上运行脚本,都可以安装cross-env软件包。然后,您可以轻松使用脚本,如下所示:
"scripts": {
"start-dev": "cross-env NODE_ENV=development nodemon --exec babel-node -- src/index.js",
"start-prod": "cross-env NODE_ENV=production nodemon --exec babel-node -- src/index.js"
}
给该软件包开发人员的大量道具。
export NODE_ENV=production
是错误的解决方案,重启后消失。
如果您不想再担心该变量,请将其添加到此文件中:
/etc/environment
不要使用导出语法,只需写(如果已有一些内容,则换行):
NODE_ENV=production
重新启动后可以工作。您将不再需要在任何地方重新输入export NODE_ENV = production命令,而只需将node与任何您想使用的东西一起使用-永远,pm2 ...
对于heroku:
heroku config:set NODE_ENV="production"
这实际上是默认设置。
heroku config:set NODE_ENV="production"
在package.json中:
{
...
"scripts": {
"start": "NODE_ENV=production node ./app"
}
...
}
然后在终端中运行:
npm start
In order to have multiple environments you need all of the answers before (NODE_ENV parameter and export it), but I use a very simple approach without the need of installing anything. In your package.json just put a script for each env you need, like this:
Then, to start the app instead of using
npm start
usenpm run script-prod
.In the code you can access the current environment with
process.env.NODE_ENV
.Voila.