Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set NODE_ENV to production/development in OS X

For use in express.js environments. Any suggestions?

like image 586
Mark Nguyen Avatar asked Feb 08 '12 17:02

Mark Nguyen


People also ask

How do you set a node environment to 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.

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 process env NODE_ENV === development?

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. Follow this answer to receive notifications.

What does NODE_ENV default to?

We see that it in fact reads NODE_ENV and defaults to 'development' if it isn't set. This variable is exposed to applications via 'app. get(“env”)' and can be used to apply environment specific configurations as explained above, but it's up to you to use this or not.


2 Answers

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.

like image 102
Farid Nouri Neshat Avatar answered Sep 30 '22 01:09

Farid Nouri Neshat


in package.json:

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

then run in terminal:

npm start 
like image 37
ethanxm Avatar answered Sep 30 '22 01:09

ethanxm