MOCKSTACKS
EN
Questions And Answers

More Tutorials









NodeJS applications Deploying in production

Setting NODE_ENV="production"


Production deployments will vary in many ways, but a standard convention when deploying in production is to define an environment variable called NODE_ENV and set its value to "production".

Runtime flags


Any code running in your application (including external modules) can check the value of NODE_ENV:

if(process.env.NODE_ENV === 'production') {
 // We are running in production mode
} else {
 // We are running in development mode
}

Dependencies


When the NODE_ENV environment variable is set to 'production' all devDependencies in your package.json file will be completely ignored when running npm install. You can also enforce this with a --production flag:

npm install --production

For setting NODE_ENV you can use any of these methods

method 1: set NODE_ENV for all node apps


Windows :

set NODE_ENV=production

Linux or other unix based system :

export NODE_ENV=production

This sets NODE_ENV for current bash session thus any apps started after this statement will have NODE_ENV set to production.

method 2: set NODE_ENV for current app


NODE_ENV=production node app.js

This will set NODE_ENV for the current app only. This helps when we want to test our apps on different environments.

method 3: create .env file and use it


Refer this post for more detailed explanation.

Basically you create .env file and run some bash script to set them on environment.

To avoid writing a bash script, the env-cmd package can be used to load the environment variables defined in the

.env file.
env-cmd .env node app.js

method 4: Use cross-env package


This package allows environment variables to be set in one way for every platform.

After installing it with npm, you can just add it to your deployment script in package.json as follows:

"build:deploy": "cross-env NODE_ENV=production webpack"


Conclusion

In this page (written and validated by ) you learned about NodeJS applications Deploying in production . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJS Securing applications.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.