是什么原因导致错误: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/lib和/usr/local/bin权限。

其他回答

它是100%的工作

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

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

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

注意所有更改/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:

sudo npm install -g create-react-app 

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

当在ubuntu中安装全局包时,你需要特殊的写入权限,因为你正在写入usr/bin文件夹。这是出于安全考虑。 所以,每次你安装一个全局包时,使用:

sudo npm install -g [package-name]

对于您的具体情况,它将是:

sudo npm install -g typescript