How to Set Node_Env to Production/Development in Os X

How to set NODE_ENV to production/development in OS X

Before running your app, you can do this in console,

export NODE_ENV=production

Or if you are in windows you could try this:

SET NODE_ENV=production

for PowerShell:

$env:NODE_ENV="production"

or you can run your app like this:

NODE_ENV=production node app.js

You can also set it in your js file:

process.env.NODE_ENV = 'production';

But I don't suggest to do it in your runtime file, since it's not easy to open up VIM in your server and change it to production. You can make a config.json file in your directory and everytime your app runs, it reads from it and sets the configuration.

How can I set NODE_ENV=production on Windows?

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

How can I set NODE_ENV=production with nodemon on Windows?

The likely problem is that the space character before && becomes part of the environment-variable values, so that the values are staging and production - note the trailing space - rather than staging and production.

The simplest way to avoid this is to remove the space before && (it looks awkward, but it works):

"scripts": {
"start": "SET NODE_ENV=staging&& nodemon app",
"production": "set NODE_ENV=production&& nodemon app",
"test": "echo \"Error: no test specified\" && exit 1"
}

To avoid such problems in general, it's best to assign variable values in cmd.exe using the form

set "variable=value"
, i.e. to enclose the name, the =, and the value in "..." overall, which delimits the value explicitly, irrespective of subsequent (unquoted) whitespace; therefore, the following should work too (with the embedded " characters escaped as \", as required by JSON).

"scripts": {
"start": "SET \"NODE_ENV=staging\" && nodemon app",
"production": "set \"NODE_ENV=production\" && nodemon app",
"test": "echo \"Error: no test specified\" && exit 1"
}

Why is process.env.NODE_ENV undefined?

process.env is a reference to your environment, so you have to set the variable there.

To set an environment variable in Windows:

SET NODE_ENV=development

on macOS / OS X or Linux:

export NODE_ENV=development

How to set environment variables from within package.json?

Set the environment variable in the script command:

...
"scripts": {
"start": "node app.js",
"test": "NODE_ENV=test mocha --reporter spec"
},
...

Then use process.env.NODE_ENV in your app.

Note: This is for Mac & Linux only. For Windows refer to the comments.

NODE_ENV after deployment becomes production?

It depends on what your deploy mechanism looks like. If you run an ec2 instance on aws it's just a dumb machine and knows nothing about how to deploy code.

As a concrete example, maybe you're running Docker and have a build step where you create your docker images. Then in your respective Dockerfile you can set NODE_ENV to production.



Related Topics



Leave a reply



Submit