是什么原因导致错误: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

当前回答

Brew卸载节点

Brew安装节点

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

希望这能帮助到

其他回答

更改您的文件权限…像这样

首先检查谁拥有这个目录

ls -la /usr/local/lib/node_modules

它拒绝访问,因为node_module文件夹属于root用户

drwxr-xr-x   3 root    wheel  102 Jun 24 23:24 node_modules

所以这需要通过将root改为你的用户来改变,但首先运行下面的命令来检查你的当前用户,我如何通过OS X中的命令行获得活动用户的名称?

id -un或者whoami

然后换主人

sudo chown -R [owner]:[owner] /usr/local/lib/node_modules

OR

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

OR

sudo chown -R $USER /usr/local/lib/node_modules

只有这样:

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

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

安装React需要超级用户级别的权限。在Linux/Unix中,超级用户帐户通常命名为“root”。

要获得超级用户权限,只需在您的终端上运行以下命令:

sudo -i

然后运行命令安装React:

npm install -g create-react-app

但是,reactjs团队鼓励我们使用以下命令,而不是安装全局包。

npx create-react-app app_name

为了尽量减少权限错误的机会,你可以配置npm使用不同的目录。在本例中,您将在主目录中创建并使用一个隐藏目录。

备份你的电脑。 在命令行中,在你的主目录中,创建一个用于全局安装的目录:

mkdir ~/.npm-global

配置npm使用新的目录路径:

npm config set prefix '~/.npm-global'

在您首选的文本编辑器中,打开或创建~/。配置文件,并添加这一行:

export PATH=~/.npm-global/bin:$PATH

在命令行,更新你的系统变量:

source ~/.profile

要测试新配置,请在不使用sudo的情况下全局安装一个包。


资料来源:NPM网站。