node.js 在 OSX 中安装 npm 模块时权限被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47252451/
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
Permission denied when installing npm modules in OSX
提问by NotoriousWebmaster
I'm trying to install node-g.raphael, and I'm getting the following error:
我正在尝试安装node-g.raphael,但出现以下错误:
Bender-03:htdocs alfred$ sudo npm install node-g.raphael --save
Password:
> [email protected] install
/Users/alfred/Sites/twistedgeo/htdocs/node_modules/contextify
> node-gyp rebuild
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir
'/Users/alfred/Sites/twistedgeo/htdocs/node_modules/contextify/build'
gyp ERR! System Darwin 16.7.0
gyp ERR! command "/usr/local/bin/node"
"/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js"
"rebuild"
gyp ERR! cwd
/Users/alfred/Sites/twistedgeo/htdocs/node_modules/contextify
gyp ERR! node -v v8.8.1
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected]
(node_modules/contextify):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected]
install: `node-gyp rebuild`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1
+ [email protected]
added 2 packages and updated 1 package in 6.556s
I've tried this solution, but it doesn't solve my problem, still getting the same error.
我试过这个解决方案,但它没有解决我的问题,仍然出现同样的错误。
Let me know if there's anything I can add to help in solving this.
让我知道是否有任何我可以添加的内容来帮助解决这个问题。
回答by antzshrek
Saw this from Fixing npm permissionsand it helped, maybe you could give it a shot as well.
从修复 npm 权限中看到了这一点,它有所帮助,也许您也可以试一试。
Option 1: Change the permission to npm's default directory
选项 1:更改 npm 默认目录的权限
Find the path to npm's directory:
npm config get prefixFor many systems, this will be
/usr/local.WARNING: If the displayed path is just
/usr, switch to Option 2or you will mess up your permissions.Change the owner of npm's directories to the name of the current user (your username):
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}This changes the permissions of the sub-folders used by npm and some other tools (
lib/node_modules,bin, andshare).
找到 npm 目录的路径:
npm config get prefix对于许多系统,这将是
/usr/local.警告:如果显示的路径只是
/usr,请切换到选项 2,否则您会弄乱您的权限。将 npm 目录的所有者更改为当前用户的名称(您的用户名):
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}这改变了由NPM使用的子文件夹的权限和一些其他的工具(
lib/node_modules,bin,和share)。
Option 2: Change npm's default directory to another directory
选项 2:将 npm 的默认目录更改为另一个目录
There are times when you do not want to change ownership of the default directory that npm uses (i.e. /usr) as this could cause some problems, for example if you are sharing the system with other users.
有时您不想更改 npm 使用的默认目录(即/usr)的所有权,因为这可能会导致一些问题,例如,如果您与其他用户共享系统。
Instead, you can configure npm to use a different directory altogether. In our case, this will be a hidden directory in our home folder.
相反,您可以将 npm 配置为完全使用不同的目录。在我们的例子中,这将是我们主文件夹中的一个隐藏目录。
Make a directory for global installations:
mkdir ~/.npm-globalConfigure npm to use the new directory path:
npm config set prefix '~/.npm-global'Open or create a
~/.profilefile and add this line:export PATH=~/.npm-global/bin:$PATHBack on the command line, update your system variables:
source ~/.profile
为全局安装创建一个目录:
mkdir ~/.npm-global配置 npm 以使用新的目录路径:
npm config set prefix '~/.npm-global'打开或创建一个
~/.profile文件并添加以下行:export PATH=~/.npm-global/bin:$PATH回到命令行,更新您的系统变量:
source ~/.profile
Test: Download a package globally without using sudo.
测试:不使用sudo.
`npm install node-g.raphael --save`
Instead of steps 2-4, you can use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):
您可以使用相应的 ENV 变量(例如,如果您不想修改~/.profile),而不是步骤 2-4 :
NPM_CONFIG_PREFIX=~/.npm-global
Option 3: Use a package manager that takes care of this for you.
选项 3:使用为您处理此问题的包管理器。
If you're doing a fresh install of Node on Mac OS, you can avoid this problem altogether by using the Homebrewpackage manager. Homebrewsets things up out of the box with the correct permissions.
如果您在 Mac OS 上全新安装 Node,则可以使用Homebrew包管理器完全避免此问题。Homebrew使用正确的权限设置开箱即用的东西。
brew install node
brew install node
I hope this helps
我希望这有帮助
回答by Jader Abreu
I use:
我用:
npm config get prefixsudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}sudo npm install
npm config get prefixsudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}sudo npm install
回答by Pankaj Garg
To get access on Mac/Linux, simply use this command
要在 Mac/Linux 上访问,只需使用此命令
sudo npm install -g {package_name}
And you are good to go.
你很高兴去。
回答by kemparaj565
Issue fixed for me by running the below command.
通过运行以下命令为我解决了问题。
sudo npm install -g @angular/cli
须藤 npm install -g @angular/cli
Was able to install angular.
能够安装角度。
Then try running ng -v to confirm.
然后尝试运行 ng -v 进行确认。

