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

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

问题描述

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

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

我已经尝试过此解决方案,但是它不能解决我的问题,仍然会出现相同的错误. /p>

让我知道我是否可以添加任何帮助解决此问题的方法.

解决方案

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

选项1:将权限更改为npm的默认目录

  1. 找到npm目录的路径:

    npm config get prefix

    对于许多系统,该名称为/usr/local.

    警告:如果显示的路径只是/usr,请切换至选项2 ,否则您的权限将会混乱.

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

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

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

选项2:将npm的默认目录更改为另一个目录

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

相反,您可以将npm配置为完全使用其他目录.对于我们来说,这将是我们主文件夹中的一个隐藏目录.

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

    mkdir ~/.npm-global

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

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

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

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

  4. 返回命令行,更新系统变量:

    source ~/.profile

测试:无需使用sudo即可全局下载软件包.

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

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

NPM_CONFIG_PREFIX=~/.npm-global

选择3:使用程序包管理器来帮您解决这个问题.

如果要在Mac OS上全新安装Node,则可以使用Homebrew程序包管理器完全避免此问题. Homebrew使用正确的权限立即设置内容.

brew install node

我希望这对您有帮助

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.

解决方案

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

Option 1: Change the permission to npm's default directory

  1. Find the path to npm's directory:

    npm config get prefix

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

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

  2. 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, and share).

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.

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. Make a directory for global installations:

    mkdir ~/.npm-global

  2. Configure npm to use the new directory path:

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

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

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

  4. Back on the command line, update your system variables:

    source ~/.profile

Test: Download a package globally without using 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):

NPM_CONFIG_PREFIX=~/.npm-global

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 install node

I hope this helps

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

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