Aakshaye
Aakshaye

Reputation: 399

Read command line arguments passed to node.js using pm2

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.

Upvotes: 5

Views: 4588

Answers (2)

soyuka
soyuka

Reputation: 9105

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

Upvotes: 6

Nepoxx
Nepoxx

Reputation: 5348

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.

Upvotes: 0

Related Questions