Command 'pm2' not found

Viewed 9254

I recently cloned my nodejs express app on ec2 ubuntu instance. I ran npm install pm2 but it didn't have write permissions. So I ran

sudo chown _R $USER /usr/lib/node_modules

After that PM2 got installed but when I hit pm2 start app.js, it shows pm2 command not found. I have installed it globally locally but nothing works. What should I do? Check out the screenshot of ERROR

3 Answers

pm2 needs to be installed globally (on the server) to function correctly.

Try

sudo npm install -g pm2

Digital Ocean has an excellent tutorial on this.

  1. in your home directory

    sudo npm install -g pm2

  2. relogin to OR restart your instance

There is a simple way to solve it, just add the pm2 statement as a new script in your package.json file.

In your root project folder type

nano package.json

Then the package file opens and you can add the following line after the script line

"pm2 start src/<yourappname.js>"

your file should look like this

"scripts": {
    "pm2": "pm2 starts src/<yourappname.js>"
  }

Then press ctl + x and enter to save

(You ust replace src/<yourappname.js> for the path adn name of your js file, in my case I have a folder called src into my project root folder)

Lastly, just run the script by typing the following line

npm run pm2

and you got it.

Related