69

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?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
kenarsuleyman
  • 920
  • 1
  • 6
  • 26

11 Answers11

127

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
num8er
  • 18,604
  • 3
  • 43
  • 57
  • 2
    Thanks so much for this clear instructions. It worked for me. I'm accepting this as answer in 5 minutes. – kenarsuleyman Jul 04 '16 at 13:20
  • 2
    can we create build for production mode using PM2? – Deep Kakkar Jan 03 '17 at 05:40
  • 2
    @DeepKakkar actually pm2 is package for running/monitoring nodejs app processes. Of You're looking for build tools, so take a look at: webpack, gulp, grunt that do automatization of deployment/packaging. PM - stands for Process Manager, not Package Manager. – num8er Jan 03 '17 at 05:46
  • 1
    can we use this like we would use an .exe application, just launch the application once. I have put question on SO. I appreciate if you please help me on http://stackoverflow.com/questions/41423800/how-do-i-create-package-build-from-node-js-for-commercial-usage – Deep Kakkar Jan 03 '17 at 05:52
  • @DeepKakkar 1) yes You can, You can create shell file or .bat,.cmd file that can run: `pm2 start app.js` 2) power of pm2 is to run multiple instances of app, prevent from stopping, monitoring. If You think to use it as desktop app, no, it's not preferred. You can single instance of Your app using it and it will prevent app from suddenly closing. But pm2 doesn't to packaging, compiling and etc. It's "brother" of `forever` package. – num8er Jan 03 '17 at 05:57
31

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
Nicks
  • 16,030
  • 8
  • 58
  • 65
  • 3
    No need to add it's path to `.bashrc`, cuz when You install it, it makes executable file `/usr/bin/pm2` or `/usr/local/bin/pm2`. Maybe in another distributive it doesn't put it there, or maybe after compiling from sources You've forgot to do: `make install` or You've copied all result manually to `/opt/node`. so in this case why not to do this: `sudo ln -s /opt/node/bin/pm2 /usr/bin/pm2` – num8er Dec 30 '16 at 10:45
  • 1
    Thanks @num8er, I will try this. Nice clarification though :-) – Nicks Jan 12 '17 at 02:43
  • For the sake of posterity, and any future visitors coming in, I would like to add that this is still working in 2022 with the latest version of `pm2`. – dmanexe Sep 21 '22 at 17:35
10

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.

Henrique Van Klaveren
  • 1,502
  • 14
  • 24
  • This was my exact problem, running Big Sur on an M1 Mac, using Zsh. After running these steps I needed to reboot my Linux Environment, but maybe I could have just run a `source` command but I wasn't sure of the format. Either way this worked for me – SeanMC Jan 02 '21 at 03:51
  • my path had npm-global as a hidden folder though `/home/ubuntu/.npm-global` – SeanMC Jan 02 '21 at 04:08
  • many thanks, you save my day – Nguyen Hoang Vu Dec 27 '21 at 06:35
3

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);
Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
3

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
Felipe Pereira
  • 1,368
  • 16
  • 26
2

This option helped me:

sudo npm i -g pm2
1

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

Andronicus
  • 25,419
  • 17
  • 47
  • 88
Ajith
  • 39
  • 2
1

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
ke deng
  • 11
  • 2
1
yum install npm -y
npm config set strict-ssl false
npm install pm2 -g
Max Coplan
  • 1,111
  • 13
  • 27
  • 3
    Hi, Welcome. Could you expand your answer to explain what these commands do and why they solve the problem in the question? As-is this answer lacks a bit of necessary context to be acceptable for the site. – Qix - MONICA WAS MISTREATED Oct 22 '21 at 06:35
1

The same as @Henrique Van Klaveren answer but simpler with using substitution:

export PATH=$PATH:$(npm config get prefix)/bin

Tropin Alexey
  • 606
  • 1
  • 7
  • 16
0
sudo npm i -g pm2

It worked for me.

slfan
  • 8,950
  • 115
  • 65
  • 78