pm2 --env

Understanding "pm2 --env" Command

As a web developer, I have come across the "pm2 --env" command multiple times while working with Node.js and PM2 process manager. In simple terms, the "pm2 --env" command is used to set environment variables for your Node.js application when running it with PM2.

How to Use "pm2 --env" Command

Before using the "pm2 --env" command, you need to make sure that you have installed PM2 on your system. Once you have done that, you can follow the below steps:

  • 1. Open your terminal or command prompt.
  • 2. Navigate to the directory where your Node.js application is located.
  • 3. Run the following command:

pm2 start app.js --env production

Here, "app.js" is the name of your main Node.js file, and "production" is the name of the environment you want to set for your application. You can replace "production" with any other environment name that you want to set.

You can also use the "pm2 env" command to see the current environment variables set for your application:


pm2 env app

Here, "app" is the name of your application that you want to check the environment variables for.

Using Multiple Environment Variables

If you have multiple environment variables that you want to set for your Node.js application, you can use the following command:


pm2 start app.js --env NODE_ENV=production,PORT=3000,DEBUG=true

Here, "NODE_ENV", "PORT", and "DEBUG" are the names of the environment variables that you want to set for your application. You can replace these with your own environment variables.

Using "pm2 set" Command

If you want to set environment variables for your application permanently, you can use the "pm2 set" command:


pm2 set env.NODE_ENV production

Here, "NODE_ENV" is the name of the environment variable that you want to set, and "production" is the value that you want to set for it.

Overall, the "pm2 --env" command is a useful tool for setting environment variables for your Node.js application when using PM2 as your process manager. It allows you to easily manage different environments for your application and improve its performance and stability.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe