Nodejs npm install pm2 -g 报错 求指教

Nodejs npm install pm2 -g 报错 求指教

OSError: [Errno 13] Permission denied
gyp ERR! configure error
gyp ERR! stack Error: gyp failed with exit code: 1
gyp ERR! stack at ChildProcess.onCpExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:337:16)
gyp ERR! stack at ChildProcess.EventEmitter.emit (events.js:98:17)
gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:789:12)
gyp ERR! System Linux 2.6.32-042stab083.2
gyp ERR! command “node” “/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js” “rebuild”
gyp ERR! cwd /usr/local/lib/node_modules/pm2/node_modules/usage
gyp ERR! node -v v0.10.24
gyp ERR! node-gyp -v v0.12.1
gyp ERR! not ok
npm http GET https://registry.npmjs.org/keypress
npm http 200 https://registry.npmjs.org/keypress
npm http GET https://registry.npmjs.org/keypress/-/keypress-0.1.0.tgz
npm http 200 https://registry.npmjs.org/keypress/-/keypress-0.1.0.tgz
npm ERR! usage@0.3.9 install: node-gyp rebuild
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the usage@0.3.9 install script.
npm ERR! This is most likely a problem with the usage package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-gyp rebuild
npm ERR! You can get their info via:
npm ERR! npm owner ls usage
npm ERR! There is likely additional logging output above.

npm ERR! System Linux 2.6.32-042stab083.2 npm ERR! command “/usr/local/bin/node” “/usr/local/bin/npm” “install” “pm2” “-g” npm ERR! cwd /usr/src/node-v0.10.24 npm ERR! node -v v0.10.24 npm ERR! npm -v 1.3.21 npm ERR! code ELIFECYCLE npm ERR! npm ERR! Additional logging details can be found in: npm ERR! /usr/src/node-v0.10.24/npm-debug.log npm ERR! not ok code 0

求教新弄了个VPS 结果安装 npm install pm2 -g

报错 请问咋回事?

补充一下 运行 npm install pm2 -g 会报错 运行 sudo npm install pm2 -g 提示 sudo:npm:找不到命令

系统是 centos6 64位


21 回复

which node 跑一下看下, 怀疑 Node 安装方式有问题, node 命令没出现在 $PATH 指定的路径里


根据你提供的错误信息和背景描述,看起来你在尝试全局安装 PM2 时遇到了权限问题。这是因为你的 Node.js 和 npm 的版本较旧(node v0.10.24 和 npm v1.3.21),并且可能没有足够的权限来执行全局安装。

以下是解决问题的步骤:

1. 使用 sudo 提升权限

首先,你可以尝试使用 sudo 命令来提升权限。你提到使用 sudo npm install pm2 -g 时提示 sudo: npm: command not found,这通常是因为 npm 没有正确安装或者不在系统的 PATH 环境变量中。你可以通过以下步骤解决这个问题:

1.1 安装或更新 npm

确保 npm 是最新版本。如果你不确定,可以先卸载再重新安装 Node.js,这样会附带安装最新版本的 npm。

# 卸载 Node.js 和 npm
sudo yum remove nodejs npm

# 安装 Node.js
sudo yum install -y nodejs

# 验证安装
node -v
npm -v

1.2 使用 sudo 安装 PM2

现在,你应该能够使用 sudo 来安装 PM2:

sudo npm install pm2 -g

2. 更新 Node.js 和 npm

考虑到你使用的 Node.js 版本非常老旧(v0.10.24),建议你升级到最新版本。Node.js 的长期支持版本(LTS)通常更稳定,并且有更好的性能和安全性。

2.1 使用 nvm(Node Version Manager)

nvm 是一个管理多个 Node.js 版本的工具,推荐使用它来安装和切换 Node.js 版本。

# 安装 nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

# 加载 nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

# 使用 nvm 安装最新 LTS 版本的 Node.js
nvm install --lts

# 验证安装
node -v
npm -v

2.2 使用 nvm 安装 PM2

npm install pm2 -g

3. 解决 node-gyp 错误

如果在安装过程中仍然遇到 node-gyp 相关的错误,可能是因为缺少编译工具。你可以通过以下命令安装这些工具:

sudo yum groupinstall "Development Tools"
sudo yum install -y python2

然后再次尝试安装 PM2:

npm install pm2 -g

通过以上步骤,你应该能够成功安装 PM2 并避免之前遇到的权限问题。

which node 返回 /usr/local/bin/node 这样对么?

我是下的源码 然后直接 make install .

我现在node能跑,forever能跑,就是pm2安装不上 好郁闷.

[@q1270989](/user/q1270989) 不明白了… npm 命令也是这样的吗?

[@jiyinyiyong](/user/jiyinyiyong) 嗯 现在就是npm install pm2 -g 报错

我安装forever express jade socket.io 全部都没问题

好郁闷

sudo !!

-g 全局安装…你没权限啊…需要sudo啊…

[@q1270989](/user/q1270989) … 难道真的因为 sudo, 前边几个命令怎么成功了

[@jiyinyiyong](/user/jiyinyiyong) 前面那几个不是全局安装…

[@jiyinyiyong](/user/jiyinyiyong) 但是我一用sudo npm 就是报错


= =!

用 sudo 提示 sudo:npm:找不到命令

[@q1270989](/user/q1270989) 多给些参数吧, 普通用户, root, 的 $PATH, 各种安装路径等等

报错没贴全的样子, 一部分我附在后边了… 汗, 没能搞定… 大致了解到的东西,

  1. 楼主是用 root 在安装模块, 所以不用 sudo
  2. sudo env 的环境 $PATH 里没有 /usr/local/bin/, 所以找不到命令… 我不是很清楚 sudo 开的进程是怎么 copy 环境变量的, 反正比较奇怪. 我尝试创建一个有 sudo 权限的账户, 结果进入 sudo 模式 $PATH 还是缺的
  3. 照网上找的说法, npm 运行 node-gyp 时候开的子进程权限可能没有 root, 所以安装过程权限不足导致这个问题…
  4. 环境是 CentOS 6.0 , Node 和 npm 都在 /usr/local/bin/
[root[@www](/user/www) ~]# npm install -g pm2
npm http GET https://registry.npmjs.org/pm2
npm http 304 https://registry.npmjs.org/pm2
npm http GET https://registry.npmjs.org/cli-table/0.2.0

npm http 304 https://registry.npmjs.org/configurable/0.0.1 npm http 304 https://registry.npmjs.org/bindings npm http 304 https://registry.npmjs.org/colors/0.3.0 npm http 304 https://registry.npmjs.org/escape-regexp/0.0.1

> usage@0.3.9 install /usr/local/lib/node_modules/pm2/node_modules/usage > node-gyp rebuild

Traceback (most recent call last): File “/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py”, line 18, in <module> sys.exit(gyp.script_main()) File “/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/init.py”, line 534, in script_main return main(sys.argv[1:]) File “/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/init.py”, line 527, in main return gyp_main(args) File “/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/init.py”, line 503, in gyp_main options.circular_check) File “/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/init.py”, line 129, in Load params[‘parallel’], params[‘root_targets’]) File “/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/input.py”, line 2687, in Load generator_input_info) File “/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/input.py”, line 594, in LoadTargetBuildFilesParallel parallel_state.pool = multiprocessing.Pool(8) File “/usr/lib64/python2.6/multiprocessing/init.py”, line 227, in Pool return Pool(processes, initializer, initargs) File “/usr/lib64/python2.6/multiprocessing/pool.py”, line 84, in init self._setup_queues() File “/usr/lib64/python2.6/multiprocessing/pool.py”, line 131, in _setup_queues self._inqueue = SimpleQueue() File “/usr/lib64/python2.6/multiprocessing/queues.py”, line 328, in init self._rlock = Lock() File “/usr/lib64/python2.6/multiprocessing/synchronize.py”, line 117, in init SemLock.init(self, SEMAPHORE, 1, 1) File “/usr/lib64/python2.6/multiprocessing/synchronize.py”, line 49, in init sl = self._semlock = _multiprocessing.SemLock(kind, value, maxvalue) OSError: [Errno 13] Permission denied gyp ERR! configure error gyp ERR! stack Error: gyp failed with exit code: 1 gyp ERR! stack at ChildProcess.onCpExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:337:16)

https://github.com/Unitech/pm2/issues/232#issuecomment-31238551

看到一个貌似跟我错误一样的 不知道pm2的人能解答不

其实我没sudo,好像也全局安装了

前面能成功是因为 npm install 的默认路径是 ~/.npm 一般都有权限…

sudo 不成功要么是脚本没有可执行权限,或者 sudo 的 PATH 跟当前帐号不一样。

http://stackoverflow.com/questions/12996397/command-not-found-when-using-sudo

跟你贴的这个issue 应该关系不大…

汗,今天遇到和你一样的事情,结果是因为root 默认环境变量里面没有/usr/local/bin,所以root找不到node和npm,sudo npm就报错了,在/etc/sudoers里面加上这个路径久可以了

怎么解决的?

从你提供的错误信息来看,问题主要在于权限不足以及版本兼容性问题。OSError: [Errno 13] Permission denied 表明当前用户没有足够的权限去写入相关的目录。

解决方案:

1. 使用管理员权限安装

由于你提到使用 sudo npm install pm2 -g 提示找不到命令,这可能是因为你的系统环境变量中没有包含 sudo 所需的路径。你可以尝试以下步骤:

sudo su -   # 切换到root用户
npm install pm2 -g

2. 使用nvm管理Node.js版本

使用Node Version Manager (nvm) 来管理你的Node.js版本,可以避免版本不兼容的问题,并且更容易管理全局包的安装权限:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
nvm install --lts
nvm use --lts
npm install pm2 -g

3. 安装Node.js和npm时使用特定版本

如果你需要安装特定版本的Node.js和npm,可以确保这些工具的版本与你的系统兼容。例如,对于CentOS 6,建议使用较旧的稳定版本:

nvm install 14.15.0
nvm use 14.15.0
npm install pm2 -g

示例代码

# 使用nvm安装和设置Node.js LTS版本
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
nvm install --lts
nvm use --lts

# 安装PM2
npm install pm2 -g

总结

通过切换到root用户或使用nvm来管理Node.js版本,可以解决权限不足及版本不兼容的问题。希望这些步骤能够帮助你成功安装PM2。

回到顶部