是什么原因导致错误: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用户安装npm:

sudo npm install -g create-react-app 

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

其他回答

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

我可以通过安装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了。

看来你遇到许可问题了。如果您正在安装npm-packages,那么在尝试全局安装包时可能会得到EACCES错误。这意味着你没有权限写入npm用来存储全局包和命令的目录。

尝试执行命令:sudo chmod u+x -R 775 ~/。npm和sudo chown $USER -R ~/。或者你可以用sudo运行任何NPM命令,这应该能解决你的问题。

如果你在本地安装npm-package,那么你应该在你的本地项目目录下,可以尝试运行sudo npm install <pkg-name>命令来安装所需的包。使用sudo的目的是改变你的所有者权限,这样你就可以授权你的当前用户运行NPM命令。

我建议你去https://docs.npmjs.com/getting-started/fixing-npm-permissions看看

Brew卸载节点

Brew安装节点

Sudo NPM install -g "your_package"——not - safe-perm=true——allow-root

希望这能帮助到

您可以通过Node版本管理器或Node安装程序安装npm。在文档中写道:

我们不建议使用Node安装程序,因为Node 安装过程将NPM安装在一个带有local的目录中 在运行NPM包时,可能会导致权限错误 在全球范围内。

NPM实际上建议使用节点版本管理器来避免这些错误。

由于您有权限错误,您可能是通过Node安装程序安装了npm,现在您需要使用nvm(节点版本管理器)重新安装它。

幸运的是,这非常简单。你甚至不需要删除当前版本的npm或Node.js。

你所需要做的就是

安装nvm。 OSX或Linux Node使用: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/<VERSION>/install.sh | bash .sh

<VERSION>应该替换为最新版本

这将在您的主目录中创建一个nvm文件夹。

Then

通过nvm安装npm和node.js。要这样做,只需打电话 NVM安装节点

(“node”是最新版本的别名)

现在你可以全局安装你的包,而不需要使用sudo或改变usr文件夹中node_modules的所有者。