node.js 如何检查我的 pm2 应用程序 NODE_ENV 是否已设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38002327/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:19:43  来源:igfitidea点击:

How can I check if my pm2 app NODE_ENV is getting set?

node.jsexpresspm2

提问by Kenmore

So I just deployed a site with node and pm2 for the first time and I'm going back and doing some optimization and reading best practices, etc.

所以我只是第一次部署了一个带有 node 和 pm2 的站点,我会回去做一些优化和阅读最佳实践等。

I read that you can get a lot of benefit by setting NODE_ENV=production.

我读到您可以通过设置NODE_ENV=production.

I found this in the pm2 docs:

我在 pm2 文档中找到了这个:

[process.json]
"env_production" : {
  "NODE_ENV": "production"
}

...

$ pm2 start process.json --env production

So, I did it but I have no idea if it is working. While trying to figure out how to check it I learned to try:

所以,我做到了,但我不知道它是否有效。在试图弄清楚如何检查它时,我学会了尝试:

$ node
> process.env.NODE_ENV
> undefined

So, that's not a good sign.. but, with my limited understanding of how the low level stuff works, I can guess that maybe pm2 launches each app as a separate node process? So maybe I'm not in the right process when I try to check it.

所以,这不是一个好兆头......但是,由于我对低级东西如何工作的理解有限,我可以猜测 pm2 可能将每个应用程序作为单独的节点进程启动?所以当我尝试检查它时,也许我没有处于正确的过程中。

Also, I don't know if I have to make a new ~/.pm2/dump.pm2 file because maybe whenever that is maybe overriding the options I set? (because I used pm2 startup).

另外,我不知道我是否必须创建一个新的 ~/.pm2/dump.pm2 文件,因为也许这可能会覆盖我设置的选项?(因为我用过pm2 startup)。

How do I check if my pm2 app's NODE_ENV is set?

如何检查我的 pm2 应用程序的 NODE_ENV 是否已设置?

回答by Kenmore

To answer the actual question in the title:

要回答标题中的实际问题:

Within your script, for me my Express app's app.jsfile, you can use process.env.NODE_ENVto get the current value of NODE_ENVand log that out if you want.

在你的脚本中,对我来说,我的 Express 应用程序app.js文件,你可以使用它process.env.NODE_ENV来获取当前值NODE_ENV并在需要时将其注销。

An even better way is to use PM2's Process Metrics module, aka pmx.

更好的方法是使用 PM2 的 Process Metrics 模块,也就是pmx.

yarn add pmx

or

或者

npm install pmx --save

then

然后

const Probe = require('pmx').probe()

Probe.metric({
  name    : 'NODE_ENV',
  value   : function() {
    return process.env.NODE_ENV
  }
})

Now it will show up in calls to pm2 monit (bottom left).

现在它将显示在对 pm2 monit 的调用中(左下角)。



To change your environment:

改变你的环境:

It is necessary that you kill and restart the process to change your environment.

您必须终止并重新启动该进程以更改您的环境。

$ pm2 kill && pm2 start pm2.json --env production

The following isn't good enough:

以下还不够好:

pm2 restart pm2.json --env production

回答by malix

Start it with npmby adding this to your package.json:

npm通过将其添加到您的package.json

"scripts": {
  "myScript": "NODE_ENV=production pm2 start server.js"
}

Then

然后

npm start myScript

You can do it directly too, but this is easy to manage, automate wth crontaband is in your source control...

你也可以直接做,但这很容易管理,自动化crontab,并且在你的源代码控制中......

回答by Roman Stetsyshin

Your process.json file is incomplete. Try using something like this:

您的 process.json 文件不完整。尝试使用这样的东西:

[process.json]
{
  "name" : "MyApp",
  "script" : "myapp.js",
  "env_production" : {
    "NODE_ENV": "production"
  }
}

Then add logging into your code, preferably somwhere on startup:

然后将日志添加到您的代码中,最好是在启动时的某个地方:

console.log("NODE_ENV : ", process.env.NODE_ENV);

Now start the application:

现在启动应用程序:

pm2 start process.json --env production

Lastly watch app logs:

最后查看应用日志:

pm2 logs MyApp

This should do it.

这应该这样做。

回答by yeiniel

May be at the start of your server script you can print the value of the environment variable and then check the PM2 logs. Use the following code to print your environment variable value:

可能在您的服务器脚本的开头,您可以打印环境变量的值,然后检查 PM2 日志。使用以下代码打印您的环境变量值:

console.log('process.env.NODE_ENV:', process.env.NODE_ENV);

And then use the following code to see the PM2 logs

然后使用以下代码查看PM2日志

pm2 logs app_name

pm2 记录 app_name

Here app_nameis your process name as indicated by the entry in the process.jsonfile.

app_nameprocess.json文件中条目所指示的进程名称。

回答by mani

You can also check your NODE_ENVvia running pm2 show <yourServerName>. This will output info about your running server including node env.

您还可以NODE_ENV通过运行来检查您的pm2 show <yourServerName>。这将输出有关您正在运行的服务器的信息,包括node env.

In addition, you can check your environment variables via running pm2 env 0. This will show all the environment variables for the running node process.

此外,您可以通过运行来检查您的环境变量pm2 env 0。这将显示正在运行的节点进程的所有环境变量。

回答by Shivam Gupta

In your terminal just type:

在您的终端中输入:

echo NODE_ENV

echo NODE_ENV

it will print current selected environment variable

它将打印当前选定的环境变量