是什么原因导致错误:EACCES:权限被拒绝,访问'/usr/local/lib/node_modules'?

npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  { Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/macbookmd101/.npm/_logs/2018-02-21T16_26_08_421Z-debug.log

当前回答

sudo chown -R $USER /usr/local/lib/node_modules . sh

其他回答

linux用户的简单解决方案,只需在你为npm运行的任何命令前添加sudo

对于linux / ubuntu如果命令

NPM install -g <package_name>

npm WARN deprecated superagent@4.1.0: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header).  This notice will go away with v5.0.2+ once it is released.

npm ERR! path ../lib/node_modules/<package_name>/bin/..

npm ERR! code EACCES

npm ERR! errno -13

npm ERR! syscall symlink

npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules
/<package_name>/bin/..' -> '/usr/local/bin/<package_name>'

npm ERR!  { [Error: EACCES: permission denied, symlink '../lib/node_modules/<package_name>/bin/..' -> '/usr/local/bin/<package_name>']

npm ERR!   cause:
npm ERR!    { Error: EACCES: permission denied, symlink '../lib/node_modules/<package_name>/bin/..' -> '/usr/local/bin/<package_name>'

npm ERR!      errno: -13,

npm ERR!      code: 'EACCES',

npm ERR!      syscall: 'symlink',

npm ERR!      path: '../lib/node_modules/<package_name>/bin/..',
npm ERR!      dest: '/usr/local/bin/ionic' },

npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied, symlink \'../lib/node_modules/ionic/bin/ionic\' -> \'/usr/local/bin/ionic\'',

npm ERR!   errno: -13,

npm ERR!   code: 'EACCES',

npm ERR!   syscall: 'symlink',

npm ERR!   path: '../lib/node_modules/<package-name>/bin/<package-name>',

npm ERR!   dest: '/usr/local/bin/<package-name>' }

npm ERR! 

npm ERR! The operation was rejected by your operating system.

npm ERR! It is likely you do not have the permissions to access this file as the current user

npm ERR! 

npm ERR! If you believe this might be a permissions issue, please double-check the

npm ERR! permissions of the file and its containing directories, or try running

npm ERR! the command again as root/Administrator (though this is not recommended).


npm ERR! A complete log of this run can be found in:

npm ERR!     /home/User/.npm/_logs/2019-07-29T01_20_10_566Z-debug.log

修复:安装与根权限

Sudo NPM install <package_name> -g

似乎你试图在全局而不是本地安装npm包,正如man npm install所描述的:

-g或——global参数将导致npm在全局而不是本地安装包。

一般来说,当你在设置一个npm项目时(在你可能有的许多其他项目中),在Node.js的全局模块(/usr/local/lib/node_modules)上安装包不是一个好主意,就像你的调试日志建议的那样。

不要使用-g,而是使用——save,它会自动将包保存为包的依赖项。json文件:

是这样的:

$ npm install express-generator --save

$ cat package.json 
{
  "name": "first_app_generator",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
},
  "author": "ivanleoncz",
  "license": "MIT",
  "dependencies": {
    "express-generator": "^4.16.0"
  }
}

但正如其他答案所提到的,如果你要使用-g,你必须在执行npm install express-generator -g时使用sudo(如果你的用户有sudo特权:请参阅/etc/sudoers),但实际上,这不是一个好主意,可能会导致权限问题。

请注意

这里有关于使用-g选项安装express-generator的说明,为了让express-cli.js脚本在系统路径上可用,但是你也可以使用本地安装的脚本,如果你的npm项目位于node_modules:

$ ./node_modules/express-generator/bin/express-cli.js --view=pug myapp

如果出现类似/usr/bin/env: ' node ': No这样的文件或目录,安装nodejs-legacy (Debian/Ubuntu)

恕我直言,如果你不确定结果,使用-g(也使用sudo)就像hic sunt dracones。

欲知更多信息:

创建NPM包(NPM init) Express.js发电机

经过长期的研究,我明白了在mac OS中安装angular cli不需要什么,只需使用sudo npm install -g @angular/cli,你的终端会提示密码,输入你的密码,然后继续安装cli。这对我很管用。

这是由于npm无法在本地运行时访问全局node_modules目录造成的

sudo chown -R Name: /usr/local/lib/node_modules

e.g.

sudo chown -R developerayo: /usr/local/lib/node_modules

修复了问题,现在您可以再次运行您运行的命令。