How to Fix pm2: command not found

Minh Vu

By Minh Vu

Updated Dec 22, 2023

Figure: How to Fix pm2: command not found

Disclaimer: All content on this website is derived directly from my own expertise and experiences. No AI-generated text or automated content creation tools are used.

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?

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
Install pm2 Globally using npm
Figure: Install pm2 Globally using npm

Make sure npm (or yarn) is also installed. If not, it might throws back the error npm: 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:

console
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:

console
5.3.0
Check pm2 Version
Figure: Check pm2 Version

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:

  1. Using the Official Installer
  2. 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:

  1. Open Terminal.

  2. 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.

  3. Close and reopen the Terminal. Now you can install Node.js using nvm.

  4. Open Terminal.

  5. Run nvm install latest to install the latest version of Node.js.

  6. Run nvm use latest to use the latest version of Node.js.

  7. 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.

Minh Vu

Minh Vu

Software Engineer

Hi guys 👋, I'm a developer specializing in Elastic Stack and Next.js. My blog shares practical tutorials and insights based on 3+ years of hands-on experience. Open to freelance opportunities — let's get in touch!

Comments

Be the first to comment!

Leave a Comment

Receive Latest Updates 📬

Get every new post, special offers, and more via email. No fee required.