简体   繁体   English

未找到 PM2 命令

[英]PM2 command not found

I installed node.js and npm to my centOS 7 server.我在我的 centOS 7 服务器上安装了 node.js 和 npm。 But i have problems with pm2.但是我对pm2有问题。 Actually real problem is i don't have experiences in linux and i don't know how to change path.实际上真正的问题是我没有 linux 的经验,我不知道如何改变路径。 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 .我通过输入cd chat进入文件夹并使用npm install pm2

After that I tried use pm2 for my server.js by typing pm2 server.js server returns "pm2 command not found".之后,我尝试通过键入pm2 server.js server 返回“pm2 command not found”来为我的 server.js 使用 pm2。 I can use node.js without any problem but pm2 not working.我可以毫无问题地使用 node.js,但 pm2 无法正常工作。

How can i solve this?我该如何解决这个问题?

Install PM2 globally:全局安装 PM2:

run as root:以root身份运行:

npm i -g pm2

or if user is sudo-er或者如果用户是 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:然后返回到用户(如果它是由 root 用户创建的,则留在 root 中)并运行它:

pm2 start server.js

PM2 the process manager for Node.js applications. PM2 Node.js 应用程序的进程管理器。 PM2 basically manages applications (run them in the background as a service). PM2 基本上管理应用程序(作为服务在后台运行它们)。 So this is how we install PM2 globally with sudo permissions account这就是我们使用 sudo 权限帐户全局安装 PM2 的方式

sudo npm install -g pm2

The -g option tells npm to install the module globally, so that it's available system-wide. -g 选项告诉 npm 全局安装模块,以便它在系统范围内可用。 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.现在,我们需要在启动 bash 脚本中添加此路径。 Add add the following line anywhere in ~/.bashrc file.在 ~/.bashrc 文件的任何位置添加以下行。

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)现在重新登录或按如下方式获取 bash 脚本(以便 bash 脚本运行并设置路径)

 source ~/.bashrc

and now it should run.现在它应该运行了。 check the status of pm2检查pm2的状态

pm2 status

In my case, I have MacOs Big Sur running with zsh shell.就我而言,我使用 zsh shell 运行 MacOs Big Sur。 The first thing you need to do is get the prefix of your npm-global path:您需要做的第一件事是获取 npm-global 路径的前缀:

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 .复制此路径,并在末尾添加 /bin -> /Users/your_user/npm-global/bin Then we will export this path into the bash configs.然后我们将此路径导出到 bash 配置中。

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

I believe all yours global npm packages will work fine now.我相信你所有的全球 npm 包现在都可以正常工作了。

Error on using port 80 with PM2?将端口 80 与 PM2 一起使用时出错?

The wrong way of going about this is trying to run with sudo .解决此问题的错误方法是尝试使用sudo运行。

The correct way of doing this would be to login as root sudo su , then run pm2 start app.js --name "whatever" --watch .这样做的正确方法是以 root 身份登录sudo su ,然后运行pm2 start app.js --name "whatever" --watch

Logging in as root, there's no need to configure any bashrc or profile files.以 root 身份登录,无需配置任何bashrc或配置文件。 However, as root, the script can use nodejs's exec() function dangerously.但是,作为 root,脚本可以危险地使用 nodejs 的exec()函数。 To avoid this, do the root stuff first with your script, then lower your privilege after some timeout:为避免这种情况,请先使用您的脚本执行 root 操作,然后在超时后降低您的权限:

// 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);

If you install through NPM and it does not work, you can create a symbolic link as well :如果你通过 NPM 安装但它不起作用,你也可以创建一个符号链接:

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

If you used nvm to install node and npm, install pm2 for normal user.如果您使用 nvm 安装 node 和 npm,请为普通用户安装 pm2。

run as root:以root身份运行:

sudo su
vim ~/.bashrc

append below code, change NVM_DIR to you normal user's home folder:附加以下代码,将 NVM_DIR 更改为普通用户的主文件夹:

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

这个选项帮助了我:

sudo npm i -g pm2

Install PM2 globally and run everything as a root user全局安装 PM2 并以 root 用户身份运行所有内容

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

You are good to go你已准备好出发

sudo npm i -g pm2

它对我有用。

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

与@Henrique Van Klaveren 的回答相同,但使用替换更简单:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM