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

当前回答

如果在授予权限后仍然不工作,请尝试运行这些命令:

mkdir ~/.npm-global

npm config set prefix '~/.npm-global'

export PATH=~/.npm-global/bin:$PATH

source ~/.profile

最后用这个命令进行测试

NPM install -g jshint

这对Windows不起作用。

其他回答

对于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

使用sudo用户安装npm:

sudo npm install -g create-react-app 

而不是 NPM install -g create-react-a pp。

更改您的文件权限…像这样

首先检查谁拥有这个目录

ls -la /usr/local/lib/node_modules

它拒绝访问,因为node_module文件夹属于root用户

drwxr-xr-x   3 root    wheel  102 Jun 24 23:24 node_modules

所以这需要通过将root改为你的用户来改变,但首先运行下面的命令来检查你的当前用户,我如何通过OS X中的命令行获得活动用户的名称?

id -un或者whoami

然后换主人

sudo chown -R [owner]:[owner] /usr/local/lib/node_modules

OR

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

OR

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

安装React需要超级用户级别的权限。在Linux/Unix中,超级用户帐户通常命名为“root”。

要获得超级用户权限,只需在您的终端上运行以下命令:

sudo -i

然后运行命令安装React:

npm install -g create-react-app

但是,reactjs团队鼓励我们使用以下命令,而不是安装全局包。

npx create-react-app app_name

解决此问题的最佳方法(因为不同设备的解决方案可能不同)是仔细阅读错误,以便找出需要写访问的文件夹。

在我的例子中,我必须给予/usr/local/lib和/usr/local/bin权限。