Pass arguments to your node.js app from pm2

In this post you will learn how to pass arguments to your node.js when you use pm2 to run your node.js application.

You can pass arguments to your node.js app using either of the following commands.

node index.js dev
npm start dev

Here I send the value β€˜dev’ to the node.js app which can be received using the following snippet.

const env = process.argv.slice(2);

I use this value to connect to different enviroments like qa, prod.

When you use pm2 to start a node.js app, you might need to send arguments in a different format.

pm2 start index.js -- dev

Anything you pass after β€” are will be sent as an array in process.argv