Set NODE_ENV via command line on Windows

Asked

Viewed 289 times

1

I am taking a Node.JS course the instructor executes the following command to change the environment variable NODE_ENV:

NODE_ENV=production node index.js

However, this command only works on Linux. On Windows, I get the following message:

'NODE_ENV' is not recognized as an Internal or External command, operable program or batch file.

How to set the environment variable and run the Node application in a single command on Windows?

3 answers

1

If you are running this from powershell (or an IDE that uses powershell as the terminal), you need to use the command

$env:NODE_ENV="production"

the old command set NODE_ENV=development && node index.js only worked in the CMD.

1

The commands are as follows::

UNIX
NODE_ENV=development node index.js

WINDOWS
set NODE_ENV=development && node index.js

0

To set the environment variable and run the Node application with a single command on Windows, we can install the npm package win-Node-env as a global dependency:

npm install -g win-node-env

Internally, it creates an application named NODE_ENV.cmd which arrow the value of the environment variable NODE_ENV and then executes the rest of the command. That way we can execute:

NODE_ENV=production node index.js

The same way we run on Linux environment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.