是什么原因导致错误: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之类的版本管理器

其他回答

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

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之类的版本管理器

我可以通过安装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用户安装npm:

sudo npm install -g create-react-app 

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

我用过sudo,但它没有解决问题, 我已经通过改变node_modules文件夹权限修复了这个问题,

sudo chmod -R 777 node_modules

如果你不为所有用户/组设置权限,你可以用任何其他代码替换777。