是什么原因导致错误: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

当前回答

注意所有更改/usr/local下所有目录所有者的响应 基本上,不要破坏Linux系统!!

对于本地的东西使用sudo也是一个非常糟糕的建议。

www.competa.com的原始链接被破坏了,所以这是原始的方法:

npm config set prefix ~/.npm

# open your .bashrc (Linux) or .bash_profile (Mac) file for editing:
nano ~/.bashrc # for Linux
# or...
nano ~/.bash_profile # for Mac if you haven't created a .bashrc file

# add these lines:
export PATH="$PATH:$HOME/npm/bin"
export NODE_PATH="$NODE_PATH:$HOME/npm/lib/node_modules"

# save the file and then enter this command or logout and login to make the changes take effect:
. ~/.bashrc
# or...
. ~/.bash_profile

选项B:使用NVM之类的版本管理器

其他回答

我可以通过安装nvm来解决这个问题,然后将node设置为最新版本。

使用curl安装nvm(最新版本请到nvm.sh)

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

列出可用节点版本

nvm ls

v10.12.0 默认值-> v10.12 (-> v10.12.0) 节点->稳定(-> v10.12.0)(默认) 稳定-> 10.12 (> v10.12.0) ...

选择要使用的节点版本

nvm use v10.12

NVM不兼容NPM配置“前缀”选项:当前设置为“”

运行此命令取消设置该选项:

nvm use --delete-prefix v10.12.0

按照上面的命令,你就可以在Ubuntu中安装react/angular了。

尝试在您尝试的任何命令之前添加sudo。

像这样:sudo NPM install

在Linux/UNIX中,将sudo与命令一起使用通常会将权限提升到超级用户级别。在Windows系统中,超级用户帐户通常被称为“Administrator”。在Linux/Unix中,超级用户帐户通常命名为“root”。

root用户拥有访问、修改或删除计算机上几乎所有文件的权限。普通用户帐户可以访问、修改或删除的文件要少得多。对普通帐户的限制可保护您的计算机免受未经授权或有害程序或用户的侵害。有些进程要求您对通常没有访问权限的文件或文件夹执行操作。安装一个每个人都可以访问的程序就是这些操作之一。

在本例中,使用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

在CentOS 8流中遇到

解决方案

(1/4)创建node_modules文件夹

$ sudo mkdir /usr/local/lib/node_modules

(2/4)与当前用户共同拥有

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

(3/4)拥有bin文件夹

$ sudo chown -R $USER /usr/local/bin/

(4/4)拥有共享文件夹

$ sudo chown -R $USER /usr/local/share/

只有这样:

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