是什么原因导致错误: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无法在本地运行时访问全局node_modules目录造成的

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

e.g.

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

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

其他回答

您可以通过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的所有者。

它是100%的工作

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

我得到了同样的错误,并解决它使用命令

Sudo NPM install -g expo-cli——unsafe-perm

在运行命令后的图像中

然后执行以下命令更新NPM

NPM install -g npm@latest

我很容易就解决了这个问题。

注意所有更改/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之类的版本管理器

在我的mac上安装Ionic时也出现了类似的问题。

我运行命令 sudo chown -R $USER /usr/local/ path你可以更新为错误给出的路径。