Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set NODE_ENV=production on Windows?

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?

like image 298
Jack Avatar asked Feb 12 '12 15:02

Jack


People also ask

How do you set a node in production?

You can signal Node. js that you are running in production by setting the NODE_ENV=production environment variable. in the shell, but it's better to put it in your shell configuration file (e.g. . bash_profile with the Bash shell) because otherwise the setting does not persist in case of a system restart.

How do I set environment variables in Windows?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

What is NODE_ENV production?

NODE_ENV is an environment variable that stands for node environment in express server. The NODE_ENV environment variable specifies the environment in which an application is running (usually, development or production).

What is the default NODE_ENV?

By default, the environment variable is unset and defaults to development.


2 Answers

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

like image 81
Jani Hartikainen Avatar answered Sep 23 '22 21:09

Jani Hartikainen


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!

like image 26
MoOx Avatar answered Sep 23 '22 21:09

MoOx