无法安装npm包||权限被拒绝

5
我在安装PM2时遇到了以下错误。我也尝试了本地安装,但似乎没有用。请帮忙解决。
命令:npm install pm2 -g

anil@anil:~/Desktop/restapis$ npm install pm2 -g 
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/pm2/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm ERR! Linux 4.4.0-78-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install" "pm2" "-g"
npm ERR! node v6.10.3
npm ERR! npm  v3.10.10
npm ERR! path ../lib/node_modules/pm2/bin/pm2
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall symlink

npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules/pm2/bin/pm2' -> '/usr/local/bin/pm2'
npm ERR!     at Error (native)
npm ERR!  { Error: EACCES: permission denied, symlink '../lib/node_modules/pm2/bin/pm2' -> '/usr/local/bin/pm2'
npm ERR!     at Error (native)
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'symlink',
npm ERR!   path: '../lib/node_modules/pm2/bin/pm2',
npm ERR!   dest: '/usr/local/bin/pm2' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/anil/Desktop/restapis/npm-debug.log
anil@anil:~/Desktop/restapis$ ^C
anil@anil:~/Desktop/restapis$ sudo chmod 777 /lib/node_modules/pm2/bin/pm2
chmod: cannot access '/lib/node_modules/pm2/bin/pm2': No such file or directory
anil@anil:~/Desktop/restapis$ x

2个回答

13

您正在尝试将PM2安装为/usr/local/bin/pm2,并且需要root权限。

不要这样做:

npm install pm2 -g 

你需要:

sudo npm install pm2 -g
请注意,您需要在sudoers组中才能操作。 或者,您可以将Node安装在家目录中,而不是系统范围内,这样您就不需要在“install -g”时使用sudo命令。

我认为使用sudo运行npm非常不好。https://medium.com/@ExplosionPills/dont-use-sudo-with-npm-still-66e609f5f92 - Thanos Mourtzoukos

1

npm 有一个名为 prefix 的配置项。该配置项告诉 npm 全局模块的安装位置。

运行以下 shell 命令:

npm config set prefix ~/.npm


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接