node.js gyp WARN EACCES 用户“root”无权访问开发目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29468404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
gyp WARN EACCES user "root" does not have permission to access the dev dir
提问by Dmitri Zaitsev
Trying to
尝试去
sudo npm install protractor -g
and the same notorious error/warning again (googled to no avail):
和同样臭名昭著的错误/警告再次(谷歌搜索无济于事):
gyp WARN EACCES user "root" does not have permission to access the dev dir "/Users/dmitrizaitsev/.node-gyp/0.12.0"
What seems to happen is that node version 0.12.0is downloaded and rebuilt, again and again during the same installation, despite of being the current node version on my machine:
node version 0.12.0尽管是我机器上的当前节点版本,但似乎发生的是在同一个安装过程中一次又一次地下载和重建:
node -v
v0.12.0
Questions:
问题:
The directory "/Users/dmitrizaitsev/.node-gyp/0.12.0" is actually missing! Why such a misleading message?
Why was this directory not created neither during the
node v0.12.0nor during the previous successful rebuild withnode-gyp?(Obviously) How can I prevent this from happening?
目录“/Users/dmitrizaitsev/.node-gyp/0.12.0”实际上丢失了!为什么会有这样一个误导性的消息?
为什么
node v0.12.0在上一次成功重建期间和期间都没有创建此目录node-gyp?(显然)我怎样才能防止这种情况发生?
I run Mac OSX 10.8.5 if that is of any importance.
如果这很重要,我会运行 Mac OSX 10.8.5。
回答by Dmitri Zaitsev
UPDATE. There is a better way - changing npm's default global directory to user sub-directory to which you already have correct permissions, so no need to mess with system file's permissions or ownership in first place.
更新。有一个更好的方法 - 将npm的默认全局目录更改为您已经拥有正确权限的用户子目录,因此无需首先弄乱系统文件的权限或所有权。
As recommended in https://docs.npmjs.com/getting-started/fixing-npm-permissions:
正如https://docs.npmjs.com/getting-started/fixing-npm-permissions 中所推荐的 :
- Make a directory for global installations:
- 为全局安装创建一个目录:
mkdir ~/.npm-global
- Configure npm to use the new directory path:
- 配置 npm 以使用新的目录路径:
npm config set prefix '~/.npm-global'
- Open or create a
~/.profile(or~/.bash_profileetc) file and add this line (at the end of the file):
- 打开或创建一个
~/.profile(或~/.bash_profile等)文件并添加此行(在文件末尾):
export PATH=~/.npm-global/bin:$PATH
- On the command line, update your system variables:
- 在命令行上,更新您的系统变量:
source ~/.profile
orsource ~/.bash_profile
或者source ~/.bash_profile
See also Sindre Sorhus's guide on the topic: https://github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md
另请参阅 Sindre Sorhus 关于该主题的指南:https: //github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md
Have now figured what was wrong:
现在已经知道出了什么问题:
The directory had wrong permissions - it was not writable(which would have been a better error message than "accessible").
该目录具有错误的权限 - 它不可写(这将是比“可访问”更好的错误消息)。
And because it was not writable, a temporary directory was used and deleted after every use, which is why the whole download had to run again and again.
而且由于不可写,每次使用后都会使用并删除一个临时目录,这就是为什么整个下载必须一次又一次地运行的原因。
The solution is to set user permissions with
解决方案是设置用户权限
sudo chown -R $USER <directory>
and never sudo npmagain.
It seems whenever you run sudo npm, all subdirectories created get wrong permissions, which will lead to problems later on.
再也不会sudo npm了。似乎无论何时运行sudo npm,创建的所有子目录都获得了错误的权限,这将导致以后出现问题。
回答by Miki Nacucchi
Try with:
尝试:
sudo npm install -g module--unsafe-perm
须藤 npm install -g模块--unsafe-perm
回答by Hyman Lee
That's because you do not have a folder in this directory "/Users/dmitrizaitsev/.node-gyp/0.12.0".
那是因为您在此目录“/Users/dmitrizaitsev/.node-gyp/0.12.0”中没有文件夹。
Just create a new folder named 0.12.0 which is the version number of your node
只需创建一个名为 0.12.0 的新文件夹,这是您节点的版本号
It will fix the problem.
它将解决问题。

