471

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?

fool-dev
  • 7,671
  • 9
  • 40
  • 54
Jack
  • 15,614
  • 19
  • 67
  • 92
  • For a multiplatform solution you can find the answer https://stackoverflow.com/a/57509175/11127383 – Daniel Danielecki Aug 15 '19 at 11:47
  • Does this answer your question? [How to set NODE\_ENV to production/development in OS X](https://stackoverflow.com/questions/9198310/how-to-set-node-env-to-production-development-in-os-x) – Michael Freidgeim Jan 04 '21 at 12:09

26 Answers26

657

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

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Jani Hartikainen
  • 42,745
  • 10
  • 68
  • 86
  • This form of configuring an environment variable did not work for me. Instead I used as described by @jsalonen and worked perfectly. – Bernardo Pacheco Jul 07 '14 at 18:10
  • 153
    For anyone still struggling with this: `set NODE_ENV=production && node app`. More conveniently configure your `package.json` accordingly: `"scripts": { "start": "set NODE_ENV=production && node app" }`. – Amberlamps Oct 20 '14 at 13:03
  • For me on Windows 8 this works `set NODE_ENV production`. Your example doesn't work on my machine. – WooCaSh Oct 20 '14 at 15:05
  • @WooCaSh sounds like you might be in PowerShell in that case, I don't think they changed command prompt's behavior in Win8 – Jani Hartikainen Oct 20 '14 at 21:48
  • @JaniHartikainen might be true. I use console from WebStorm. Thanks for advice. – WooCaSh Oct 21 '14 at 10:16
  • is there anything returns after you type in this command? I didn't get anything at all after I type in this command. (I am trying to run a node project on Windows too) – Aaron Liu Apr 21 '15 at 03:25
  • 5
    @ShuruiLiu the command will not output anything, but you can type `echo %NODE_ENV%` to check its current value. – Jani Hartikainen Apr 26 '15 at 05:31
  • 221
    Heads up: "set NODE_ENV=production && " adds a trailing space to the variable. I needed "set NODE_ENV=production&& " to avoid the additional space which breaks node apps like Ghost. – daw May 15 '15 at 10:58
  • 18
    @Amberlamps that is not a good solution because the NODE_ENV is then just hardcoded for all machines; the real goal is to change the env by machine using an env variable or to pass in the value at the command line, not hardcode it in the package.json file. – Alexander Mills May 16 '15 at 06:00
  • 1
    @AlexMills that's why you can add more than one "scripts" item. simply add one for "debug" or whatever you wanna call it and run that on other machines – Jörn Berkefeld Jun 26 '15 at 14:25
  • using this to switch from development to testing is inconvenient because once you use "set NODE_ENV=testing&& server" then you can't just hit ctrl+c and run node server expecting NODE_ENV to default to 'development' because you just set it to 'testing' for the terminal session. Would be great if node had a --env option that just overrides NODE_ENV temporarily. – isimmons Jul 12 '15 at 21:09
  • 14
    I think using `cross-env` is the better solution to this problem if your team works on mixed operating systems. Answer from @MoOx would be my pick as an answer to this question. – philk Mar 09 '16 at 09:52
  • 5
    Thanks @daw. Your comments should be added to the original answer – RPDeshaies Feb 14 '17 at 15:59
  • 1
    The token '&&' is not a valid statement separator in this version. – SeanMC Nov 05 '17 at 19:33
  • 1
    Thanks @daw again and set NODE_ENV='production' && does not work either b/c the quote will also be read into NODE_ENV – Qiulang May 03 '18 at 08:26
  • @daw you actually saved me from wasting more time. thanks – Han K Sep 21 '21 at 06:26
363

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MoOx
  • 8,423
  • 5
  • 40
  • 39
187

In PowerShell:

$env:NODE_ENV="production"
jsalonen
  • 29,593
  • 15
  • 91
  • 109
  • 4
    p.s.: don't forget the $ and the quotes ;) – George Nov 22 '16 at 20:52
  • 8
    `set NODE_ENV=production` did not work for me in powershell but this did. Thanks! – rage Apr 11 '17 at 02:56
  • 9
    Struggled a bit to get this to work from Powershell within Visual Studio Code. Thought I'd leave solution here. I was attempting to run a "Gulp" command, while ensuring correct env value was set. This is what wound up working for me: `$env:NODE_ENV="development"; gulp runMytask`. Note the semi-colon in there. The gulp file can use conditional logic on process.env.NODE_ENV. Unless you set it, it will be undefined. – dvsoukup Sep 28 '17 at 15:20
  • 2
    Only this solution work for me with windows 10 and webpack 3.8.1 – Роман Арсеньев Oct 22 '17 at 15:40
  • 1
    This works perfect. But, the `cross-env NODE_ENV=production` option is really a better solution if running npm commands from package.json that require the env to be set. It's too easy to leave the env set on dev/prod after using the $env:NODE_ENV option – Drenai Dec 26 '17 at 17:35
174

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"
    }
    
Barry MSIH
  • 3,525
  • 5
  • 32
  • 53
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
41

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brieuc P
  • 475
  • 4
  • 2
  • 3
    How can we execute another command with the variable set? This does not seem to work: npm run env NODE_ENV=production && echo $NODE_ENV. Probably they are executed in two different shells? – Jonas Kello Nov 12 '15 at 12:59
  • 1
    From what I can see, this doesn't work at all. From the docs itself, It's only **listing** environment variables, not setting them. – kumarharsh Dec 30 '15 at 08:58
  • 2
    Does not work for me. It list vars, show the var you specify, but on runtime, var is not ok in process.env.YOUR_VAR... – MoOx Feb 10 '16 at 05:51
  • 4
    @JonasKello You would use this: `npm run env NODE_ENV=production -- node -e 'console.log(process.env.NODE_ENV)'` The `--` is **mandatory**. Replace `node -e 'console.log(process.env.NODE_ENV)'` with whatever command you want. – Pauan Oct 20 '16 at 13:54
  • Tried this with this command: `npm run env NODE_TLS_REJECT_UNAUTHORIZED=0 -- node --inspect ./etc/http-req-standalone.js` and ... nothing happened. I'm not sure this method works on windows. – jcollum Jul 19 '17 at 17:53
  • This method does not work on windows, but replacing `--` with `&&` should work. `npm run env NODE_ENV=production && node -e 'console.log(process.env.NODE_ENV)'` – Kevin Farrugia Oct 31 '17 at 10:51
  • This worked for me using [cmder](https://cmder.net/) (which essentially uses cmd.exe / PowerShell under the hood) on Windows: `npm run env NODE_ENV=development -- node app.js` – eriegz Apr 13 '21 at 22:30
23

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
laggingreflex
  • 32,948
  • 35
  • 141
  • 196
  • 2
    exactly what I was looking for! to run with npm scripts, and it even works with other node cli tools like jest. So "set NODE_ENV=debug & cls & jest..." became "cls & NODE_ENV=debug jest" – Bernardo Dal Corno Sep 14 '18 at 16:40
14

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
edhubbell
  • 2,218
  • 1
  • 16
  • 17
14

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
paul
  • 21,653
  • 1
  • 53
  • 54
13

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.

Mattatat-tat
  • 459
  • 1
  • 7
  • 9
  • 3
    Thank you! I had done all this but it wasn't working until I restarted the server. – Marcel Lamothe Feb 07 '19 at 15:17
  • Question - will next.js be able to read these windows system environment variables? We're deploying to Azure using CI/CD pipelines and want to take advantage of the Configuration Settings in an Azure Web App, which your code (at least in .NET) can read just like reading windows system environment variables without any code changes. Hope this makes sense :| – Speedcat Jan 18 '22 at 17:51
10

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

$env:NODE_ENV="production"
kukab
  • 561
  • 5
  • 18
9

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cameron Yick
  • 708
  • 1
  • 8
  • 16
9

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
8

first in powershell type

$env:NODE_ENV="production"

then type

node fileName.js

It will work perfectly displaying all the outputs.

Abhinav
  • 167
  • 2
  • 12
3

For Windows

set NODE_ENV=development && react-scripts start

For Ubuntu, Linux, macOs

NODE_ENV=development react-scripts start

  • Welcome to StackOverflow! Please edit your answer to explain what this code does and how it answers the question, so that it is useful to people with similar issues. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Todd Jul 09 '21 at 15:55
  • How it answers the question? This is exactly the way to rewrite the command in windows. I don't see what other explanation is needed here. – GiselleMtnezS Feb 09 '23 at 22:11
2

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.

Dominik
  • 2,283
  • 1
  • 25
  • 37
  • .env files are only convenient for secrets and add an extra step of complexity when setting up when assigning new people to a project or applying changes to them. – coiso Mar 31 '18 at 12:25
  • @coiso Without a generic, common place, where do you put *many* env variables? Either in a script file or in an IDE dependent setting, you're even more bound to specific tools. This makes integrating new team members even more harder, I think. – Dominik Apr 03 '18 at 08:55
2

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

Bozhinovski
  • 2,496
  • 3
  • 20
  • 38
2
set NODE_ENV=production & node server.js 
S.B
  • 13,077
  • 10
  • 22
  • 49
Talbi Said
  • 39
  • 3
1

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

0

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.

Alok Rajasukumaran
  • 381
  • 1
  • 5
  • 20
0

You can install the next plugin now that your commands are operational.

npm install -g win-node-env
-1

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
Bar Horing
  • 5,337
  • 3
  • 30
  • 26
-1

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

NODE_ENV=testcases npm run seed-db

-1

In Windows 10 I have used $env:NODE_ENV="Production"

This worked on my vscode terminal. But if your server is listening first need to stop and set your env variable and start the app again. This way it worked for me.

arun-dev
  • 9
  • 2
-1

If you are looking to set environment variable and run npm script in the same line then use:

$env:BASE_URL="https://example.com"; npm run __your_npm_script__
Imran Latif
  • 985
  • 8
  • 21
-1

For git bash terminal in vscode it is:export NODE_ENV, instead of set NODE_ENV

  • In addition to this being a question specifically about Windows shells, this was already given as an answer [5 years ago](https://stackoverflow.com/a/51861824/1255289) – miken32 Apr 06 '23 at 18:39
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 13 '23 at 05:51
-3

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

Aimery
  • 1,559
  • 1
  • 19
  • 24
  • 1
    You've probably overcome this, but your last line would be: `ps aux | grep NODE_ENV` But, you're also not running with the "start_windows" script. Other answers point out that you'd need to do something like: `"set NODE_ENV=test&& node ./index.js"`. Finally, you're setting the envar for that particular session, on that terminal. So, really, your echo or more proper "ps aux" op is invalid. You'd just log-out the setting in your "index.js" or "app.js", whatever your entry is: `console.log(process.env.NODE_ENV);`. Anyway, I hope that helps more than a downvote. – Neil Gaetano Lindberg Nov 17 '20 at 21:06
  • 2
    This is not an answer, but a question. – Nemanja Milosavljevic Nov 11 '21 at 16:05