是什么原因导致错误: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
为了尽量减少权限错误的机会,你可以配置npm使用不同的目录。在本例中,您将在主目录中创建并使用一个隐藏目录。
备份你的电脑。
在命令行中,在你的主目录中,创建一个用于全局安装的目录:
mkdir ~/.npm-global
配置npm使用新的目录路径:
npm config set prefix '~/.npm-global'
在您首选的文本编辑器中,打开或创建~/。配置文件,并添加这一行:
export PATH=~/.npm-global/bin:$PATH
在命令行,更新你的系统变量:
source ~/.profile
要测试新配置,请在不使用sudo的情况下全局安装一个包。
资料来源:NPM网站。
对于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