node.js 如何在 Windows 上设置 NODE_ENV=production?

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

How can I set NODE_ENV=production on Windows?

node.jsexpress

提问by Hyman

In Ubuntu it's quite simple; I can run the application using:

在 Ubuntu 中,这很简单;我可以使用以下方法运行应用程序:

$ NODE_ENV=production node myapp/app.js

However, this doesn't work on Windows. Is there a configuration file where I can set the attribute?

但是,这不适用于 Windows。是否有可以设置属性的配置文件?

回答by Jani Hartikainen

Current versions of Windows use Powershell as the default shell, so use:

当前版本的 Windows 使用 Powershell 作为默认 shell,因此请使用:

$env:NODE_ENV="production"

Per @jsalonen's answer below. If you're in CMD (which is no longer maintained), use

根据@jsalonen 下面的回答。如果您使用 CMD(不再维护),请使用

set NODE_ENV=production

This should be executed in the command prompt where you intend to run your Node.js application.

这应该在您打算运行 Node.js 应用程序的命令提示符中执行。

The above line would set the environment variable NODE_ENV for the command prompt where you execute the command.

上面的行将为您执行命令的命令提示符设置环境变量 NODE_ENV。

To set environment variables globally so they persist beyond just the single command prompt, you can find the tool from System in Control Panel (or by typing 'environment' into the search box in the start menu).

要全局设置环境变量,使其在单个命令提示符之外持续存在,您可以从“控制面板”中的“系统”中找到该工具(或通过在开始菜单的搜索框中键入“环境”)。

回答by MoOx

I just found a nice Node.js package that can help a lot to define environment variables using a unique syntax, cross platform.

我刚刚找到了一个很好的 Node.js 包,它可以帮助很多使用独特的跨平台语法定义环境变量。

https://www.npmjs.com/package/cross-env

https://www.npmjs.com/package/cross-env

It allow you to write something like this:

它允许你写这样的东西:

cross-env NODE_ENV=production my-command

Which is pretty convenient! No Windows or Unix specific commands any more!

这很方便!不再有 Windows 或 Unix 特定的命令!

回答by jsalonen

In PowerShell:

在 PowerShell 中:

$env:NODE_ENV="production"

回答by Alexander Mills

It would be ideal if you could set parameters on the same line as your call to start Node.js on Windows. Look at the following carefully, and run it exactly as stated:

如果您可以在调用在 Windows 上启动 Node.js 的同一行上设置参数,那将是理想的。仔细查看以下内容,并完全按照说明运行它:

You have these two options:

您有以下两种选择:

  1. At the command line:

    set NODE_ENV=production&&npm start
    

    or

    set NODE_ENV=production&&node index.js
    
  2. The trick for it to work on Windows is you need to remove the whitespace before and after the "&&".Configured your package.json file with start_windows (see below) below. Then Run "npm run start_windows" at the command line.

    //package.json
    
    "scripts": {
      "start": "node index.js"
      "start_windows": "set NODE_ENV=production&&node index.js"
    }
    
  1. 在命令行:

    set NODE_ENV=production&&npm start
    

    或者

    set NODE_ENV=production&&node index.js
    
  2. 它在 Windows 上工作的技巧是您需要删除“&&”前后的空格。使用下面的 start_windows(见下文)配置您的 package.json 文件。然后在命令行运行“npm run start_windows”。

    //package.json
    
    "scripts": {
      "start": "node index.js"
      "start_windows": "set NODE_ENV=production&&node index.js"
    }
    

回答by Brieuc P

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 run-script 文档中

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. If an "env" command is defined in your package it will take precedence over the built-in.

env 脚本是一个特殊的内置命令,可用于列出脚本在运行时可用的环境变量。如果在您的包中定义了“env”命令,它将优先于内置命令。

回答by paul

If you are using Visual Studio with NTVS, you can set the environment variables on the project properties page:

如果您使用带有 NTVS 的 Visual Studio,您可以在项目属性页面上设置环境变量:

Visual Studio NTVS Project Properties

Visual Studio NTVS 项目属性

As you can see, the Configuration and Platform dropdowns are disabled (I haven't looked too far into why this is), but if you edit your .njsprojfile as follows:

如您所见,“配置”和“平台”下拉菜单已禁用(我还没有深入研究为什么会这样),但是如果您.njsproj按如下方式编辑文件:

  <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
    <DebugSymbols>true</DebugSymbols>
    <Environment>NODE_ENV=development</Environment>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
    <DebugSymbols>true</DebugSymbols>
    <Environment>NODE_ENV=production</Environment>
  </PropertyGroup>

The 'Debug / Release' dropdown will then control how the variable is set before starting Node.js.

然后,“调试/发布”下拉列表将控制在启动 Node.js 之前如何设置变量。

回答by laggingreflex

I wrote a module win-node-envwith which you can run your command just like you would in *nix.

我写了一个模块win-node-env,你可以用它运行你的命令,就像在 *nix 中一样。

NODE_ENV=production node myapp/app.js

It works by creating a NODE_ENV.cmdthat sets the NODE_ENVenvironment variable and spawns a child process with the rest of the command and its args.

它的工作原理是创建一个NODE_ENV.cmd设置NODE_ENV环境变量并使用其余命令及其参数生成子进程的。

Just install it (globally), and run your npm script commands, it should automatically make them work.

只需安装它(全局),然后运行您的 npm 脚本命令,它就会自动使它们工作。

npm install -g win-node-env

回答by edhubbell

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. AND you have to restart Visual Studio in order for the new value to be recognized.

从 cmd 窗口。并且您必须重新启动 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 时返回“undefined”,如果使用 setx 并重新启动 Visual Studio,则返回“development”。

回答by Mattatat-tat

Here is the non-command line method:

这是非命令行方法:

In Windows 7 or 10, type environment into the start menu search box, and select Edit the system environment variables.

在 Windows 7 或 10 中,在开始菜单搜索框中键入环境,然后选择编辑系统环境变量。

Alternatively, navigate to Control Panel\System and Security\System, and click Advanced system settings

或者,导航到控制面板\系统和安全\系统,然后单击高级系统设置

This should open up the System properties dialog box with the Advanced tab selected. At the bottom, you will see an Environment Variables... button. Click this.

这应该打开系统属性对话框,选择高级选项卡。在底部,您将看到一个环境变量...按钮。点击这个。

System Dialog Box

系统对话框

The Environment Variables Dialog Box will open.

环境变量对话框将打开。

Environment Variable Dialog Box

环境变量对话框

At the bottom, under System variables, select New...This will open the New System Variable dialog box.

在底部的系统变量下,选择新建...这将打开新建系统变量对话框。

enter image description here

在此处输入图片说明

Enter the variable name and value, and click OK.

输入变量名称和值,然后单击“确定”。

You will need to close all cmd prompts and restart your server for the new variable to be available to process.env. If it still doesn't show up, restart your machine.

您需要关闭所有 cmd 提示并重新启动服务器以使新变量可用于 process.env。如果它仍然没有出现,请重新启动您的机器。

回答by pstrawberriedev

Just to clarify, and for anyone else that may be pulling their hair out...

只是为了澄清,对于其他任何可能正在拔头发的人......

If you are using git bash on Windows, set node_env=production&& node whatever.jsdoes not seem to work. Instead, use the native cmd. Then, using set node_env=production&& node whatever.jsworks as expected.

如果您在 Windows上使用git bashset node_env=production&& node whatever.js似乎不起作用。相反,请使用本机 cmd。然后,使用set node_env=production&& node whatever.js按预期工作。

My use case:

我的用例:

I develop on Windows because my workflow is a lotfaster, but I needed to make sure that my application's development-specific middleware were not firing in the production environment.

我在Windows上开发,因为我的工作流程是很多快,但我需要确保我的应用程序的开发专用的中间件并没有在生产环境中射击。