node.js 有没有办法从命令行显示 NODE_ENV?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27935533/
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
Is there a way to display NODE_ENV from the command line?
提问by thetrystero
Please, answers here all refer to a something called process.env.NODE_ENV, but echo $process.env.NODE_ENVfrom the command line did not work. Any ideas?
拜托,这里的答案都指的是一个叫做 process.env.NODE_ENV 的东西,但echo $process.env.NODE_ENV从命令行不起作用。有任何想法吗?
回答by Peter Lyons
Use echo $NODE_ENV. The command line is a shell, probably bash and that's the bash syntax to print the value of an environment variable.
使用echo $NODE_ENV. 命令行是一个 shell,可能是 bash,这是打印环境变量值的 bash 语法。
回答by user2720864
If you have defined NODE_ENVvariable then you should be able to see this by typing nodein the command prompt which will open the node cell and then type process.env.NODE_ENV.
如果您已经定义了NODE_ENV变量,那么您应该能够通过node在命令提示符中键入来打开节点单元格然后键入process.env.NODE_ENV.
To check the existing env variables .. type this .. process.env
要检查现有的环境变量 .. 输入这个 .. process.env
回答by stephan
To display the current node environment in windows, use:
要在 Windows 中显示当前节点环境,请使用:
> echo %NODE_ENV%
It wil output the environment on the command line like:
它将在命令行上输出环境,如:
development
回答by stephan
go to node REPL, and then give process.env.NODE_ENV and the variable process is scoped inside nodejs process, not in your shell process.
转到节点 REPL,然后提供 process.env.NODE_ENV 变量 process 范围在 nodejs 进程内,而不是在您的 shell 进程中。
sk3037@saravana:~/src$ node
> process.env.
回答by Mark A
You call list all variables for Macs available for your project directory with...
您调用列表所有可用于您的项目目录的 Mac 变量...
printenv
I use this often to look for the NODE_ENV and other variables.
我经常使用它来查找 NODE_ENV 和其他变量。
回答by Pumych
Setp-by-step windows CMD NODE_ENV:
逐步设置窗口 CMD NODE_ENV:
set NODE_ENV=my_node_env (defines NODE_ENV)
node (run node)
process.env.NODE_ENV (show NODE_ENV)
设置 NODE_ENV=my_node_env(定义 NODE_ENV)
节点(运行节点)
process.env.NODE_ENV(显示 NODE_ENV)
After "set NODE_ENV" you can run the application, and it will use the set NODE_ENV. You can run your application with custom environment in pm2without problem.
在“设置 NODE_ENV”之后,您可以运行应用程序,它将使用设置的 NODE_ENV。您可以毫无问题地在pm2 中使用自定义环境运行您的应用程序。
回答by brainsiq
- Find the Id of the process you are running by executing
ps aux | grep node - Look at the environment variables used by that process by executing
less /proc/[your-id]/environ
- 通过执行找到您正在运行的进程的 ID
ps aux | grep node - 通过执行查看该进程使用的环境变量
less /proc/[your-id]/environ
回答by pmverma
Have you set the NODE_ENV for process?
您是否为进程设置了 NODE_ENV?
Here are some example.
Somewhere in code, you set the node environment to "production"or "development"or "any thing you want".
And do some stuff according to your node environment.
这里有一些例子。在代码中的某处,您将节点环境设置为"production"or"development"或"any thing you want"。并根据您的节点环境做一些事情。
process.env.NODE_ENV="production";
//others coding
if(process.env.NODE_ENV === "production")
{
//useblabla log level.
//use production log.
}
else if(process.env.NODE_ENV === "development")
{
//useblabla log level.
//use development log.
}
console.log(process.env.NODE_ENV); //"production"

