I am using pm2 to start my app but i am not able to pass argument to it. the command I am using is pm2 start app.js -- dev. Though this works with forever.
I have tested and it works in my windows machine. Below is the complete solution to pass arguments to nodejs app using pm2.
** There are also 2 types of argument
There are 2 ways to pass arguments with pm2.
Option 1: pass by argument with pm2 commands.
Option 2: by using config file e.g ecosystem.config.js
Option 1 (Pass arg by commands):
pm2 start app/myapp1.js --node-args="--max-http-header-size=80000" -- arg1 arg2
//Access the arg as below in your node program.
console.log(process.argv[2]); // arg1
console.log(process.argv[3]); // arg2
Option 2 (Using config file): If you are using ecosystem.config.js. you can define with the following configuration:
{
name: 'my-app',
script: 'app\\myapp1.js',
env: {
NODE_ENV: 'DEV',
PORT : 5051
},
node_args: '--max-http-header-size=80000',
args : 'arg1 arg2',
instances: 1,
exec_mode: 'fork'
}
To start as dev mode:
pm2 start --name myapp app/myapp1.js -- .\ecosystem.config.js
To start as production mode, just add --env=production
pm2 start --name myapp app/myapp1.js -- .\ecosystem.config.js --env=production
//Access the arg as below in your node program.
console.log(process.argv[2]); // arg1
console.log(process.argv[3]); // arg2