The command I run on my server to start my node app is:
我在服务器上运行的启动节点app的命令是:
sudo IS_PROD=1 node app.js
I have forever installed but can't seem to pass in the environment variable.
我已经安装好了,但似乎无法通过环境变量。
sudo IS_PROD=1 forever node app.js
Doesn't seem to do the trick. I have tried several varieties of this. How do I either execute this command successfully or permanently set the environment variable?
似乎并没有达到目的。我试过好几种。如何成功地执行此命令或永久地设置环境变量?
2 个解决方案
#1
77
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. forever automatically starts your script using nodejs. Instead you should do like this;
首先,您应该跳过命令中的节点,它不应该在那里,您不应该能够执行它。forever自动使用nodejs启动脚本。相反,你应该这样做;
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
.
这两种方法都保留了环境变量,就像使用节点时一样。
#2
12
app.js:
app.js:
console.log(process.env.IS_PROD);
Using node
(v0.8.21)
使用节点(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)
使用永远(v0.10.0)
$ forever app.js
undefined
$ IS_PROD=1 forever app.js
1
$ sudo IS_PROD=1 forever app.js
1
文档:
process.env
process.env
An object containing the user environment. See environ(7).
包含用户环境的对象。看到环境(7)。
#1
77
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. forever automatically starts your script using nodejs. Instead you should do like this;
首先,您应该跳过命令中的节点,它不应该在那里,您不应该能够执行它。forever自动使用nodejs启动脚本。相反,你应该这样做;
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
.
这两种方法都保留了环境变量,就像使用节点时一样。
#2
12
app.js:
app.js:
console.log(process.env.IS_PROD);
Using node
(v0.8.21)
使用节点(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)
使用永远(v0.10.0)
$ forever app.js
undefined
$ IS_PROD=1 forever app.js
1
$ sudo IS_PROD=1 forever app.js
1
文档:
process.env
process.env
An object containing the user environment. See environ(7).
包含用户环境的对象。看到环境(7)。