How to set NODE_ENV to production/development in OS X

How to set NODE_ENV to production/development in OS X

For use in express.js environments. Any suggestions?

Solutions/Answers:

Solution 1:

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

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.

Solution 2:

in package.json:

{
  ...
  "scripts": {
    "start": "NODE_ENV=production node ./app"
  }
  ...
}

then run in terminal:

npm start

Solution 3:

No one mentioned .env in here yet? Make a .env file in your app root, then require('dotenv').config() and read the values. Easily changed, easily read, cross platform.

https://www.npmjs.com/package/dotenv

Solution 4:

export NODE_ENV=production is bad solution, it disappears after restart.

if you want not to worry about that variable anymore – add it to this file:

/etc/environment

don’t use export syntax, just write (in new line if some content is already there):

NODE_ENV=production

it works after restart. You will not have to re-enter export NODE_ENV=production command anymore anywhere and just use node with anything you’d like – forever, pm2…

For heroku:

heroku config:set NODE_ENV="production"

which is actually default.

Solution 5:

This is a good article about NODE_ENV: http://www.hacksparrow.com/running-express-js-in-production-mode.html.

For automatically setting from Grunt you can use https://npmjs.org/package/grunt-env plugin.

Solution 6:

heroku config:set NODE_ENV="production"