在 OSX 中安装 npm 模块时权限被拒绝 [英] Permission denied when installing npm modules in OSX

查看:25
本文介绍了在 OSX 中安装 npm 模块时权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试安装 node-g.raphael,但出现以下错误:

I'm trying to install node-g.raphael, and I'm getting the following error:

Bender-03:htdocs alfred$ sudo npm install node-g.raphael --save
Password:

> contextify@0.1.15 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: contextify@0.1.15 
  (node_modules/contextify):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: contextify@0.1.15 
  install: `node-gyp rebuild`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1

+ node-g.raphael@0.0.9
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.

推荐答案

修复 npm 权限 并且它有所帮助,也许您也可以尝试一下.

Saw this from Fixing npm permissions and it helped, maybe you could give it a shot as well.

  1. 找到 npm 目录的路径:

  1. Find the path to npm's directory:

npm config 获取前缀

对于许多系统,这将是 /usr/local.

For many systems, this will be /usr/local.

警告:如果显示的路径只是/usr,请切换到Option 2,否则你会搞乱你的权限.

WARNING: If the displayed path is just /usr, switch to Option 2 or you will mess up your permissions.

将 npm 目录的所有者更改为当前用户的名称(您的用户名):

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}

这会更改 npm 和其他一些工具(lib/node_modulesbinshare)使用的子文件夹的权限.

This changes the permissions of the sub-folders used by npm and some other tools (lib/node_modules, bin, and share).

选项 2:将 npm 的默认目录更改为其他目录

有时您不想更改 npm 使用的默认目录(即 /usr)的所有权,因为这可能会导致一些问题,例如,如果您与其他人共享系统用户.

Option 2: Change npm's default directory to another directory

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 配置为完全使用不同的目录.在我们的例子中,这将是我们主文件夹中的一个隐藏目录.

Instead, you can configure npm to use a different directory altogether. In our case, this will be a hidden directory in our home folder.

  1. 为全局安装创建一个目录:

  1. Make a directory for global installations:

mkdir ~/.npm-global

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

Configure npm to use the new directory path:

npm 配置设置前缀'~/.npm-global'

打开或创建一个 ~/.profile 文件并添加这一行:

Open or create a ~/.profile file and add this line:

导出路径=~/.npm-global/bin:$PATH

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

Back on the command line, update your system variables:

source ~/.profile

测试:不使用 sudo 全局下载包.

Test: Download a package globally without using sudo.

`npm install node-g.raphael --save`

您可以使用相应的 ENV 变量代替步骤 2-4(例如,如果您不想修改 ~/.profile):

Instead of steps 2-4, you can use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):

NPM_CONFIG_PREFIX=~/.npm-global

选项 3:使用为您处理此问题的包管理器.

如果您在 Mac OS 上全新安装 Node,您可以使用 Homebrew 包管理器完全避免这个问题.Homebrew 使用正确的权限设置开箱即用的东西.

Option 3: Use a package manager that takes care of this for you.

If you're doing a fresh install of Node on Mac OS, you can avoid this problem altogether by using the Homebrew package manager. Homebrew sets things up out of the box with the correct permissions.

brew 安装节点

希望能帮到你

这篇关于在 OSX 中安装 npm 模块时权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆