5

I'm aware about how to pass variables to node.js using pm2. But how do I read them? process.argv doesn't contain it.

This is what I'm referring to.

Passing environment variables to node.js using pm2

UPDATE

pm2 start file_name.js -- -my_port 8080 is the right way to do it. process.argv will contain the arguments.

But running pm2 describe file_name still shows args -3000 which is a cached value. Restarting the system gives me the argument that was passed last before restart, which was 3000 in my case.

Community
  • 1
  • 1
Aakshaye
  • 359
  • 1
  • 3
  • 17

2 Answers2

6

I think you're confusing :

  • node_args

node_args list ["--harmony", "--max-stack-size=1024"] arguments given to node when it is launched

Those are node executable options, like --harmony or --debug=7001. For more informations see node --help

  • args

args list ["--enable-logs", "-n", "15"] arguments given to your app when it is launched

Those are your script arguments. In a json declaration it's the arg property but within command line the syntax is:

pm2 start app.js -- arg1 arg2

Those should be available in process.argv.

Reference

soyuka
  • 8,839
  • 3
  • 39
  • 54
  • Thanks, I was a bit confused. It's the command line arguments that I want to pass. After running the following command pm2 start fetch.js -- -port 3000 I got this in process.argv [ '/usr/bin/nodejs', '/usr/lib/node_modules/pm2/lib/FetchFork.js', '-port', '80' ] – Aakshaye Jan 06 '16 at 07:09
  • Port 80 is what I had passed as an argument yesterday. So clearly there is some kind of caching going on here. – Aakshaye Jan 06 '16 at 07:12
  • Yes once the process is in memory arguments might stay the same. Restart it with new arguments if it's already started. – soyuka Jan 06 '16 at 09:24
0

From the Node.js doc about process.argv:

An array containing the command line arguments.

It does not contain environment variables. You can access the ENV_VARIABLE environment variable using

process.env.ENV_VARIABLE

See this answer.

Community
  • 1
  • 1
Nepoxx
  • 4,849
  • 5
  • 42
  • 61