In this tutorial, I will help you to fix the pm2: command not found error the easiest way.
In short, the pm2: command not found error occurs because the pm2 package is not recognized by the terminal, which means it is not installed yet, or not installed globally.
So the solution is to install it using sudo npm install -g pm2
or sudo yarn global add pm2
.
To understand more, you can read the content below.
Contents
- What is pm2?
- Why pm2: command not found Error Occurs?
- Fixing pm2: command not found Error
- Checking if pm2 is Installed Successfully
- Installing Node.js (Optional)
- Conclusion
What is pm2?
In case you are following some tutorials on the Internet and don't know what pm2 is, let me explain it to you.
pm2 is a process manager for Node.js applications. It helps you to:
- Run an application and keep it alive.
- Reload the application without downtime.
- Perform common sys-admin tasks.
For my use case, I usually use pm2 to run my NextJs application in a VPS hosted on DigitalOcean.
Why pm2: command not found Error Occurs?
The most popular reason why you encounter the pm2: command not found error is that the pm2 package is not installed, or not installed GLOBALLY.
Thus, the terminal cannot recognize the pm2
command, and it throws back the error pm2: command not found
.
Fixing pm2: command not found Error
To fix the pm2: command not found error, you will need to install the pm2 package globally.
You can open your Terminal (in Linux or MacOS), and run one of the following commands to install the pm2 package globally:
-
If you use
npm
(or have no idea what you are using), run:console sudo npm install -g pm2
-
If you use
yarn
, run:console sudo yarn global add pm2

Make sure
npm
(oryarn
) is also installed. If not, it might throws back the errornpm: command not found
and you will need to install Node.js first.
Checking if pm2 is Installed Successfully
To check if pm2 is installed successfully, run the following command:
pm2 --version
If it returns the version of pm2, then you have installed pm2 successfully.
For example, at the time I write this post, the latest version of pm2 is 5.3.0
, so it returns:
5.3.0

Installing Node.js (Optional)
As mentioned before, if it throws back the error npm: command not found
, it means you don't have Node.js installed on your machine. So you will need to install Node.js first.
Node.js will provide us with the npm
package manager, so we need to install Node.js.
To install Node.js, there are 2 ways:
- Using the Official Installer
- Using the Node Version Manager (nvm): Windows, Linux/MacOS
It's easier to use the installer, just download and follow the instructions. So I won't write the instructions here.
But if you want to use nvm, I will show you how to install Node.js using nvm on Linux/MacOS.
Installing Node.js using nvm on Linux/MacOS
Follow these steps to install nvm on Linux/MacOS:
-
Open Terminal.
-
Run the following command:
console curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
The version at the time I write this post is
v0.39.7
. I suggest you to visit the official guide to update the latest version. -
Close and reopen the Terminal. Now you can install Node.js using nvm.
-
Open Terminal.
-
Run
nvm install latest
to install the latest version of Node.js. -
Run
nvm use latest
to use the latest version of Node.js. -
Run
sudo npm install -g pm2
to install pm2 globally.
That's all, now you have installed Node.js and pm2 successfully on Linux/MacOS.
Conclusion
To recap, you can fix the pm2: command not found error by installing the pm2 package globally using npm install -g pm2
or yarn global add pm2
.
In case you don't have Node.js on your machine, I also added the instructions to install Node.js using the official installer and nvm.
Thanks for reading! Please comment below if you need any more assistance.
Comments
Be the first to comment!