PM2 command not found

2020-02-19 08:02发布

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 linux
8条回答
甜甜的少女心
2楼-- · 2020-02-19 08:31

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
查看更多
The star\"
3楼-- · 2020-02-19 08:35

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);
查看更多
登录 后发表回答