PM2 command not found

Install PM2 globally:

run as root:

npm i -g pm2

or if user is sudo-er

sudo npm i -g pm2

and then go back to user (or stay in root if it was created by root user) and run it:

pm2 start server.js

PM2 the process manager for Node.js applications. PM2 basically manages applications (run them in the background as a service). So this is how we install PM2 globally with sudo permissions account

sudo npm install -g pm2

The -g option tells npm to install the module globally, so that it's available system-wide. Once this is installed, check the installed path as:

whereis pm2
pm2: /opt/node/bin/pm2 /opt/node/lib/node_modules/pm2/bin/pm2

Now, we need to add this path in startup bash script. Add add the following line anywhere in ~/.bashrc file.

export PATH=$PATH:/opt/node/lib/node_modules/pm2/bin

Now re-login or source the bash script as follows(so that bash script runs and path is set)

 source ~/.bashrc

and now it should run. check the status of pm2

pm2 status

In my case, I have MacOs Big Sur running with zsh shell. The first thing you need to do is get the prefix of your npm-global path:

npm config get prefix

Then this will be return some thing like this:

/Users/your_user/npm-global

Copy this path, and add the /bin in the end -> /Users/your_user/npm-global/bin. Then we will export this path into the bash configs.

export PATH=$PATH:/Users/your_user/npm-global/bin 

I believe all yours global npm packages will work fine now.