How can I set NODE_ENV=production on Windows?

node.jsExpress

node.js Problem Overview


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

$ 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?

node.js Solutions


Solution 1 - node.js

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

$env:NODE_ENV="production"

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

set NODE_ENV=production

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

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

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).

Solution 2 - node.js

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

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!

Solution 3 - node.js

In PowerShell:

$env:NODE_ENV="production"

Solution 4 - node.js

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:

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"
     }
    

Solution 5 - node.js

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.

From the npm run-script documentation:

> 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.

Solution 6 - node.js

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

NODE_ENV=production node myapp/app.js

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

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

npm install -g win-node-env

Solution 7 - node.js

My experience using Node.js on Windows 7 64-bit in Visual Studio 2013 is that you need to use

setx NODE_ENV development

from a cmd window. AND you have to restart Visual Studio in order for the new value to be recognized.

The set syntax only lasts for the duration of the cmd window in which it is set.

Simple test in 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.

Solution 8 - node.js

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

Visual Studio NTVS Project Properties

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 .njsproj file as follows:

  <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.

Solution 9 - node.js

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.

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.

Solution 10 - node.js

To run your application in PowerShell (since && is disallowed):

($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.)

Solution 11 - node.js

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.js does not seem to work. Instead, use the native cmd. Then, using set node_env=production&& node whatever.jsworks as expected.

My use case:

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

Solution 12 - node.js

first in powershell type

$env:NODE_ENV="production"

then type

node fileName.js

It will work perfectly displaying all the outputs.

Solution 13 - node.js

if you are using vs code terminal you have to use this command

$env:NODE_ENV="production"

Solution 14 - node.js

For multiple environment variables, an .env file is more convenient:

# .env.example, committed to repo
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3

# .env, private, .gitignore it
DB_HOST=real-hostname.example.com
DB_USER=real-user-name
DB_PASS=REAL_PASSWORD

It's easy to use with dotenv-safe:

  1. Install with npm install --save-dev dotenv-safe.
  2. Include it in your code (best at the start of the index.js) and directly use it with the process.env command:

require('dotenv').load()
console.log(process.env.DB_HOST)   

Don't forget to ignore the .env file in your VCS.

Your program then fails fast if a variable "defined" in .env.example is unset either as an environment variable or in .env.

Solution 15 - node.js

In case you are using GITBASH terminal "set NODE_ENV=production" will not work, what can you do is type "export NODE_ENV=production"

Solution 16 - node.js

For Windows

set NODE_ENV=development && react-scripts start

For Ubuntu, Linux, macOs

NODE_ENV=development react-scripts start

Solution 17 - node.js

Restart VS code if the NODE_ENV or any other environment variable is not providing correct value. This should work after restart.

Solution 18 - node.js

this will not set a variable but it's usefull in many cases. I will not recommend using this for production, but it should be okay if you're playing around with npm.

npm install --production

Solution 19 - node.js

I used npm script for running a gulp task without "&&"

NODE_ENV=testcases npm run seed-db

Solution 20 - node.js

set NODE_ENV=production & node server.js 

Solution 21 - node.js

Finally, the best method I could see is as follows.

"set-env-qa": "npm run env APP_ENV=qa",
"start:qa": "npm run set-env-qa && react-native start",

This will make sure we get the correct env setup for the programs. replace react-native-start with whichever next command you want.

Solution 22 - node.js

It seems that

{
   "start_windows": "set NODE_ENV=test"
}

is not working for me. I'm currently trying this on my Windows machine. When I hit:

npm run start_windows

it would execute on the console without errors but when I try to echo

echo %NODE_ENV%

nothing comes out of it, meaning it does not exist and it wasn't set at all...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJackView Question on Stackoverflow
Solution 1 - node.jsJani HartikainenView Answer on Stackoverflow
Solution 2 - node.jsMoOxView Answer on Stackoverflow
Solution 3 - node.jsjsalonenView Answer on Stackoverflow
Solution 4 - node.jsAlexander MillsView Answer on Stackoverflow
Solution 5 - node.jsBrieuc PView Answer on Stackoverflow
Solution 6 - node.jslaggingreflexView Answer on Stackoverflow
Solution 7 - node.jsedhubbellView Answer on Stackoverflow
Solution 8 - node.jspaulView Answer on Stackoverflow
Solution 9 - node.jsMattatat-tatView Answer on Stackoverflow
Solution 10 - node.jsCameron YickView Answer on Stackoverflow
Solution 11 - node.jspstrawberriedevView Answer on Stackoverflow
Solution 12 - node.jsAbhinavView Answer on Stackoverflow
Solution 13 - node.jskukabView Answer on Stackoverflow
Solution 14 - node.jsDominikView Answer on Stackoverflow
Solution 15 - node.jsBozhinovskiView Answer on Stackoverflow
Solution 16 - node.jsVijay GawariyaView Answer on Stackoverflow
Solution 17 - node.jsRakesh PahujaView Answer on Stackoverflow
Solution 18 - node.jsBar Horing AmirView Answer on Stackoverflow
Solution 19 - node.jsMari OrlovaView Answer on Stackoverflow
Solution 20 - node.jsTalbi SaidView Answer on Stackoverflow
Solution 21 - node.jsAlok RajasukumaranView Answer on Stackoverflow
Solution 22 - node.jsdada delos angelesView Answer on Stackoverflow