⚠️ This lesson is retired and might contain outdated information.

Passing environment variables to node.js using pm2

Will Button
InstructorWill Button
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 2 years ago

In this video, you will learn how to pass environment variables to your node.js app using the pm2 config file. This is useful for setting options inside your app such as production or development.

[00:02] PM2 can also pass environment variables to our Node apps. Once again, we're going to go into our configuration file. We're going to add an object called ENV, which accepts a JavaScript object. Inside of there, we're going to set our Node_ENV variable equal to production. We can save that and exit.

[00:32] If we start our app up, our app is up and running. Watching is enabled. Now, if we edit app1 server.js, and if we do something like add process.env.NODE_ENV, and we concatenate that inside of our string, we save that, PM2 list is going to show us that PM2 reloaded the app after detecting change in the files. If we refresh our page, we get, "Hello from app1 production," which is the value of the environment variable we passed in.

egghead
egghead
~ 36 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today