PM2 command not found

node.jsLinux

node.js Problem Overview


I installed node.js and npm to my centOS 7 server. But i have problems with pm2. Actually real problem is i don't have experiences in linux and i don't know how to change path. Here is folder structure.

* bin
* code
* error_docs
* httpdocs
* lib64
* logs
* tmp
* var
* chat(my node.js folder)
    * node_modules
        * pm2
        * sockjs
    * server.js
* dev
* etc
* lib
* local
* sbin
* usr

I entered folder by typing cd chat and installed pm2 with npm install pm2.

After that I tried use pm2 for my server.js by typing pm2 server.js server returns "pm2 command not found". I can use node.js without any problem but pm2 not working.

How can i solve this?

node.js Solutions


Solution 1 - node.js

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

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

Solution 3 - node.js

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.

Solution 4 - node.js

Error on using port 80 with PM2?

The wrong way of going about this is trying to run with sudo.

The correct way of doing this would be to login as root sudo su, then run pm2 start app.js --name "whatever" --watch.

Logging in as root, there's no need to configure any bashrc or profile files. However, as root, the script can use nodejs's exec() function dangerously. To avoid this, do the root stuff first with your script, then lower your privilege after some timeout:

// I use port 80 first.. at this point the script's UID is root.

app.listen(80);

// After 2 seconds we switch to UID `azureuser`, which obviously isn't root anymore.

setTimeout(function() {
  process.setuid("azureuser");
}, 2000);

Solution 5 - node.js

If you install through NPM and it does not work, you can create a symbolic link as well :

ln -s /<your-user>/.npm-global/lib/node_modules/pm2/bin/pm2 /usr/bin/pm2

After that, you're going to be able to call:

pm2

Solution 6 - node.js

If you used nvm to install node and npm, install pm2 for normal user.

run as root:

sudo su
vim ~/.bashrc

append below code, change NVM_DIR to you normal user's home folder:

export NVM_DIR="/home/[PLEASE CHANGE]/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  
# This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. 
"$NVM_DIR/bash_completion"  
# This loads nvm bash_completion

at last :

source ~/.bashrc

Solution 7 - node.js

This option helped me:

sudo npm i -g pm2

Solution 8 - node.js

Install PM2 globally and run everything as a root user

sudo apt-get install npm
sudo npm i -g pm2
sudo ln -s /usr/bin/nodejs /usr/bin/node

You are good to go

Solution 9 - node.js

sudo npm i -g pm2

It worked for me.

Solution 10 - node.js

yum install npm -y
npm config set strict-ssl false
npm install pm2 -g

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkenarsuleymanView Question on Stackoverflow
Solution 1 - node.jsnum8erView Answer on Stackoverflow
Solution 2 - node.jsNicksView Answer on Stackoverflow
Solution 3 - node.jsHenrique Van KlaverenView Answer on Stackoverflow
Solution 4 - node.jsservercharlieView Answer on Stackoverflow
Solution 5 - node.jsFelipe PereiraView Answer on Stackoverflow
Solution 6 - node.jske dengView Answer on Stackoverflow
Solution 7 - node.jsAlexander EzhovView Answer on Stackoverflow
Solution 8 - node.jsAjithView Answer on Stackoverflow
Solution 9 - node.jsHimanshu PatniView Answer on Stackoverflow
Solution 10 - node.jsuser17213844View Answer on Stackoverflow