Passing environment variables to node.js using pm2
Asked Answered
M

6

16

I am trying to pass some arguments to my Express application which is run by pm2. There wasn't any hint in their documentation to do so, but apparently it's possible to pass some EV to your node application like SOME_STUFF=xxx pm2 start app.js.

Malediction answered 11/11, 2015 at 18:5 Comment(0)
E
15

It's actually possible and I'm pretty sure it was in PM2's documentation some time ago.

Anyways, that's what you need to do:

pm2 start app.js -- -some_stuff xxx

Basically, add -- and then you can add your own app parameters.

Managed to find the source, it was hidden quite well: http://pm2.keymetrics.io/docs/usage/quick-start/#42-ways-of-starting-processes

Ensoul answered 11/11, 2015 at 18:11 Comment(3)
Wierd, this doesn't work for me. but SOME_STUFF=xxx pm2 start app.js worked. Any thoughts why ?Panter
@Ensoul thanks a lot. spent two hours trying different methods, luckily ended up here.Bursary
Just confirming that using " -- " resolved my issue loading Azure App Service ENV variables to PM2 from a custom container. This is vital to include....Pteranodon
P
32

Note - after updating environment variables in your environment, you must do the following:

pm2 restart all --update-env

Also look for a .env file in the node source directory.

Peat answered 11/1, 2020 at 20:1 Comment(0)
E
15

It's actually possible and I'm pretty sure it was in PM2's documentation some time ago.

Anyways, that's what you need to do:

pm2 start app.js -- -some_stuff xxx

Basically, add -- and then you can add your own app parameters.

Managed to find the source, it was hidden quite well: http://pm2.keymetrics.io/docs/usage/quick-start/#42-ways-of-starting-processes

Ensoul answered 11/11, 2015 at 18:11 Comment(3)
Wierd, this doesn't work for me. but SOME_STUFF=xxx pm2 start app.js worked. Any thoughts why ?Panter
@Ensoul thanks a lot. spent two hours trying different methods, luckily ended up here.Bursary
Just confirming that using " -- " resolved my issue loading Azure App Service ENV variables to PM2 from a custom container. This is vital to include....Pteranodon
P
12

The environment variables don't always update unless you force them to.

SOME_STUFF=xxx pm2 start app.js --update-env
Powerdive answered 23/7, 2021 at 10:48 Comment(0)
A
11

I was having issues passing parameters using pm2 start app.js -- -some_stuff xxx so I opted to do this instead: SOME_STUFF=xxx OTHER_STUFF=abc pm2 start app.js.

Then when I ran pm2 logs I was able to see that my app successfully started and that the environment variables were set correctly where as before I was seeing errors around these variables when I ran pm2 logs.

Amphitheater answered 30/12, 2019 at 17:47 Comment(0)
P
6

You should pass ENV in ecosystem.config.js

ecosystem.config.js (in the root)

module.exports = {
  apps: [
    {
      name: "project-name",
      exec_mode: "cluster",
      instances: "1",
      script: "./server/index.js", // your script
      args: "start",
      env: {
        NODE_ENV: "production", 
        SOME_ENV: "some_value"...
      },
    },
  ],
};

In the console:

pm2 start ecosystem.config.js

There is information about configuration of ENV in PM2 official documentation

Pneumogastric answered 17/4, 2021 at 18:39 Comment(2)
can i just load a .env and .env.local somehow?Section
I needed to run pm2 start ecosystem.config.js, instead of pm2 run ...Reisfield
S
1

My node app (sveltekit build) starts in my ubuntu server when I use

node build/index.js

and includes enviroment variables

so with pm2 I found that my app starts with envs starting it:

pm2 "node build/index.js"
Sikhism answered 5/8, 2022 at 8:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.