How to set NODE_ENV to production/development in OS X

Javascriptnode.jsMacosEnvironment Variables

Javascript Problem Overview


For use in express.js environments. Any suggestions?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

in package.json:

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

then run in terminal:

npm start

Solution 3 - Javascript

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

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

To not have to worry whether you are running your scripts on Windows, Mac or Linux install the cross-env package. Then you can use your scripts easily, like so:

"scripts": {
    "start-dev": "cross-env NODE_ENV=development nodemon --exec babel-node -- src/index.js",
    "start-prod": "cross-env NODE_ENV=production nodemon --exec babel-node -- src/index.js"
}

Massive props to the developers of this package.

npm install --save-dev cross-env

Solution 6 - Javascript

heroku config:set NODE_ENV="production"

Solution 7 - Javascript

For Windows Powershell use this command

$env:NODE_ENV="production" ; node app.js

Solution 8 - Javascript

On OSX I'd recommend adding export NODE_ENV=development to your ~/.bash_profile and/or ~/.bashrc and/or ~/.profile.

Personally I add that entry to my ~/.bashrc and then have the ~/.bash_profile ~/.profile import the contents of that file, so it's consistent across environments.

After making these additions, be sure to restart your terminal to pick up settings.

Solution 9 - Javascript

In order to have multiple environments you need all of the answers before (NODE_ENV parameter and export it), but I use a very simple approach without the need of installing anything. In your package.json just put a script for each env you need, like this:

...
"scripts": {
    "start-dev": "export NODE_ENV=dev && ts-node-dev --respawn --transpileOnly ./src/app.ts",
    "start-prod": "export NODE_ENV=prod && ts-node-dev --respawn --transpileOnly ./src/app.ts"
  }
 ...

Then, to start the app instead of using npm start use npm run script-prod.

In the code you can access the current environment with process.env.NODE_ENV.

Voila.

Solution 10 - Javascript

Windows CMD -> set NODE_ENV=production

Windows Powershell -> $env:NODE_ENV="production"

MAC -> export NODE_ENV=production

Solution 11 - Javascript

If you are on windows. Open your cmd at right folder then first

set node_env={your env name here}

hit enter then you can start your node with

node app.js

it will start with your env setting

Solution 12 - Javascript

If you using webpack in your application, you can simply set it there, using DefinePlugin...

So in your plugin section, set the NODE_ENV to production:

plugins: [
  new webpack.DefinePlugin({
    'process.env.NODE_ENV': '"production"',
  })
]

Solution 13 - Javascript

npm start --mode production
npm start --mode development

With process.env.NODE_ENV = 'production'

Solution 14 - Javascript

Daniel has a fantastic answer which is the better approach for the correct deployment (set and forget) process.

For those using express. You can use grunt-express-server which is fantastic as well. https://www.npmjs.org/package/grunt-express-server

Solution 15 - Javascript

You can run by environment as below,

NODE_ENV=production npm run start

Solution 16 - Javascript

I don't see a Docker solution explicitly mentioned anywhere; hopefully this helps someone:

In Dockerfile:

FROM node:16.11-alpine3.14 AS production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark NguyenView Question on Stackoverflow
Solution 1 - JavascriptFarid Nouri NeshatView Answer on Stackoverflow
Solution 2 - JavascriptwangchiView Answer on Stackoverflow
Solution 3 - JavascripttheflowersoftimeView Answer on Stackoverflow
Solution 4 - JavascriptLukas LiesisView Answer on Stackoverflow
Solution 5 - JavascriptNotoriousView Answer on Stackoverflow
Solution 6 - Javascriptdavid_adlerView Answer on Stackoverflow
Solution 7 - JavascriptHarikrishnanView Answer on Stackoverflow
Solution 8 - JavascriptVincil BishopView Answer on Stackoverflow
Solution 9 - JavascriptrmptView Answer on Stackoverflow
Solution 10 - JavascriptJanith UdaraView Answer on Stackoverflow
Solution 11 - JavascriptgarenyondemView Answer on Stackoverflow
Solution 12 - JavascriptAlirezaView Answer on Stackoverflow
Solution 13 - JavascriptTính Ngô QuangView Answer on Stackoverflow
Solution 14 - JavascriptJesseView Answer on Stackoverflow
Solution 15 - JavascriptFurkan ÖztürkView Answer on Stackoverflow
Solution 16 - JavascriptrinogoView Answer on Stackoverflow