本文翻译自:How can I set NODE_ENV=production on Windows?
In Ubuntu it's quite simple; 在Ubuntu中,这非常简单。 I can run the application using: 我可以使用以下命令运行该应用程序:
$ NODE_ENV=production node myapp/app.js
However, this doesn't work on Windows. 但是,这在Windows上不起作用。 Is there a configuration file where I can set the attribute? 是否可以在其中配置属性的配置文件?
参考:https://stackoom.com/question/coIo/如何在Windows上设置NODE-ENV-production
在PowerShell中:
$env:NODE_ENV="production"
My experience using Node.js on Windows 7 64-bit in Visual Studio 2013 is that you need to use 我在Visual Studio 2013的Windows 7 64位上使用Node.js的经验是您需要使用
setx NODE_ENV development
from a cmd window. 从cmd窗口。 AND you have to restart Visual Studio in order for the new value to be recognized. 并且,您必须重新启动Visual Studio才能识别新值。
The set syntax only lasts for the duration of the cmd window in which it is set. 设置语法仅在设置它的cmd窗口期间持续。
Simple test in Node.js: Node.js中的简单测试:
console.log('process.env.NODE_ENV = ' + process.env.NODE_ENV);
It returns 'undefined' when using set, and it will return 'development' if using setx and restarting Visual Studio. 使用set时,它将返回“未定义”,如果使用setx并重新启动Visual Studio,则将返回“开发”。
It would be ideal if you could set parameters on the same line as your call to start Node.js on Windows. 如果可以在Windows上启动Node.js的调用所在的行中设置参数,那将是理想的选择。 Look at the following carefully, and run it exactly as stated: 仔细查看以下内容,并严格按照说明运行它:
You have these two options: 您有以下两种选择:
At the command line: 在命令行中:
set NODE_ENV=production&&npm start
or 要么
set NODE_ENV=production&&node index.js
The trick for it to work on Windows is you need to remove the whitespace before and after the "&&". 在Windows上运行的技巧是您需要在“ &&”之前和之后删除空格。 Configured your package.json file with start_windows (see below) below. 使用下面的start_windows(请参见下文)配置了package.json文件。 Then Run "npm run start_windows" at the command line. 然后在命令行中运行“ npm run start_windows”。
//package.json "scripts": { "start": "node index.js" "start_windows": "set NODE_ENV=production&&node index.js" }
To run your application in PowerShell (since &&
is disallowed): 要在PowerShell中运行您的应用程序(因为不允许&&
):
($env:NODE_ENV="production") -and (node myapp/app.js)
Note that the text output of what the server's doing is suppressed, and I am not sure if that's fixable. 请注意,服务器正在执行的操作的文本输出被抑制,因此我不确定这是否可以解决。 (Expanding on @jsalonen's answer.) (扩展@jsalonen的答案。)
You can use 您可以使用
npm run env NODE_ENV=production
It is probably the best way to do it, because it's compatible on both Windows and Unix. 这可能是最好的方法,因为它在Windows和Unix上都兼容。
From the npm run-script documentation : 从npm运行脚本文档中 :
The env script is a special built-in command that can be used to list environment variables that will be available to the script at runtime. env脚本是一个特殊的内置命令,可用于列出运行时脚本可使用的环境变量。 If an "env" command is defined in your package it will take precedence over the built-in. 如果您的软件包中定义了“ env”命令,它将优先于内置命令。