Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Passing environment variables to node.js using pm2

    Will ButtonWill Button

    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.

    pm2PM2
    nodeNode.js
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    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.

    Discuss

    Discuss