Skip to content

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

See also  Add JWT Authentication to your Swagger API docs

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.