简体   繁体   English

在 OSX 中安装 npm 模块时权限被拒绝

[英]Permission denied when installing npm modules in OSX

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:

> 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.修复 npm 权限中看到了这一点,它有所帮助,也许您也可以 试一试

Option 1: Change the permission to npm's default directory选项 1:更改 npm 默认目录的权限

  1. Find the path to npm's directory:找到 npm 目录的路径:

    npm config get prefix

    For many systems, this will be /usr/local .对于许多系统,这将是/usr/local

    WARNING : If the displayed path is just /usr , switch to Option 2 or you will mess up your permissions.警告:如果显示的路径只是/usr ,请切换到选项 2,否则您会弄乱您的权限。

  2. Change the owner of npm's directories to the name of the current user (your username):将 npm 目录的所有者更改为当前用户的名称(您的用户名):

    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 ).这会更改 npm 和其他一些工具( lib/node_modulesbinshare )使用的子文件夹的权限。

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 (ie /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.相反,您可以将 npm 配置为完全使用不同的目录。 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 以使用新的目录路径:

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

  3. Open or create a ~/.profile file and add this line:打开或创建一个~/.profile文件并添加以下行:

    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 .测试:在不使用sudo情况下全局下载包。

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

Instead of steps 2-4, you can use the corresponding ENV variable (eg 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 Homebrew package manager.如果您在 Mac OS 上全新安装 Node,则可以使用Homebrew包管理器完全避免这个问题。 Homebrew sets things up out of the box with the correct permissions. Homebrew使用正确的权限设置开箱即用的东西。

brew install node

I hope this helps我希望这有帮助

我使用:

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

Rather than changing your file permissions and risking confusion within your system (what I have done and may live to regret at a later date), may I suggest the following that I found after the fact.与其更改您的文件权限并冒着在您的系统中造成混乱的风险(我所做的并且可能会在以后后悔),我可以建议我在事后发现的以下内容。

Resolving EACCES permissions errors when installing packages globally 解决全局安装包时的 EACCES 权限错误

To get access on Mac/Linux, simply use this command要在 Mac/Linux 上访问,只需使用此命令

sudo npm install -g {package_name}

And you are good to go.你很高兴去。

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 进行确认。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM