Node.js forever with environment variable

为君一笑 提交于 2019-12-02 17:26:49

First of all you should skip the node thing in you command, it should not be there, you should not be able to execute that. automatically starts your script using . Instead you should do like this;

sudo IS_PROD=1 forever app.js

Probably you, instead of starting your server in foreground, will want to start your server as a daemon. eg.

sudo IS_PROD=1 forever start app.js

This will create a process in the background that will watch your node app and restart it when it exits. For more information see the readme.

Both of these methods preserves the environment variables, just like when you are just using node.

app.js:

console.log(process.env.IS_PROD);

Using node (v0.8.21)

$ node app.js
undefined

$ IS_PROD=1 node app.js
1

$ sudo IS_PROD=1 node app.js
1

Using forever (v0.10.0)

$ forever app.js
undefined

$ IS_PROD=1 forever app.js
1

$ sudo IS_PROD=1 forever app.js
1

Documentation:

process.env

An object containing the user environment. See environ(7).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!