markdown 如果你已经在Mac OS X或Linuxbrew上通过Homebrew安装了Node,那么如何修复npm的说明

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 如果你已经在Mac OS X或Linuxbrew上通过Homebrew安装了Node,那么如何修复npm的说明相关的知识,希望对你有一定的参考价值。

# Fixing npm On Mac OS X for Homebrew Users

Installing node through Homebrew can cause problems with npm for globally installed packages. To fix it quickly, use the solution below. An explanation is also included at the end of this document.

## Solution

This solution fixes the error caused by trying to run `npm update npm -g`. Once you're finished, you also won't need to use `sudo` to install npm modules globally.

Before you start, make a note of any globally installed npm packages. These instructions will have you remove all of those packages. After you're finished you'll need to re-install them.

Run the following commands to remove all existing global npm modules, uninstall node & npm, re-install node with the correct defaults, configure the location for global npm modules to be installed, and then install npm as its own pacakge.

```bash
rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node --without-npm
echo prefix=~/.npm-packages >> ~/.npmrc
curl -L https://www.npmjs.com/install.sh | sh
```

Node and npm should be correctly installed at this point. The final step is to add `~/.npm-packages/bin` to your `PATH` so npm and global npm packages are usable. To do this, add the following line to your `~/.bash_profile`:

```bash
export PATH="$HOME/.npm-packages/bin:$PATH"
```

Now you can re-install any global npm packages you need without any problems.

## Explanation of the issue

If you're a Homebrew user and you installed node via Homebrew, there is a major philosophical issue with the way Homebrew and NPM work together. If you install node with Homebrew and then try to do `npm update npm -g`, you may see an error like this:

```
$ npm update npm -g
npm http GET https://registry.npmjs.org/npm
npm http 304 https://registry.npmjs.org/npm
npm http GET https://registry.npmjs.org/npm/1.4.4
npm http 304 https://registry.npmjs.org/npm/1.4.4
npm ERR! error rolling back Error: Refusing to delete: /usr/local/bin/npm not in /usr/local/lib/node_modules/npm
npm ERR! error rolling back     at clobberFail (/usr/local/Cellar/node/0.10.26/lib/node_modules/npm/lib/utils/gently-rm.js:57:12)
npm ERR! error rolling back     at next (/usr/local/Cellar/node/0.10.26/lib/node_modules/npm/lib/utils/gently-rm.js:43:14)
npm ERR! error rolling back     at /usr/local/Cellar/node/0.10.26/lib/node_modules/npm/lib/utils/gently-rm.js:52:12
npm ERR! error rolling back     at Object.oncomplete (fs.js:107:15)
npm ERR! error rolling back  npm@1.4.4 { [Error: Refusing to delete: /usr/local/bin/npm not in /usr/local/lib/node_modules/npm] code: 'EEXIST', path: '/usr/local/bin/npm' }
npm ERR! Refusing to delete: /usr/local/bin/npm not in /usr/local/lib/node_modules/npm
File exists: /usr/local/bin/npm
Move it away, and try again. 

npm ERR! System Darwin 13.1.0
npm ERR! command "/usr/local/Cellar/node/0.10.26/bin/node" "/usr/local/bin/npm" "update" "npm" "-g"
npm ERR! cwd /Users/dan/Google Drive/Projects/dotfiles
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.3
npm ERR! path /usr/local/bin/npm
npm ERR! code EEXIST
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/dan/Google Drive/Projects/dotfiles/npm-debug.log
npm ERR! not ok code 0

```

There's an [NPM bug for this exact problem](https://github.com/npm/npm/issues/3794). The bug has been "fixed" by Homebrew installing npm in a way that allows it to manage itself once the install is complete. However, this is error-prone and still seems to cause problems for some people. The root of the the issue is really that `npm` is its own package manager and it is therefore better to have `npm` manage itself and its packages completely on its own instead of letting Homebrew do it. 

Also, using the Homebrew installation of `npm` will require you to use `sudo` when installing global packages. Since one of the core ideas behind Homebrew is that apps can be installed without giving them root access, this is a bad idea.

以上是关于markdown 如果你已经在Mac OS X或Linuxbrew上通过Homebrew安装了Node,那么如何修复npm的说明的主要内容,如果未能解决你的问题,请参考以下文章

markdown Mac OS X设置。

markdown 在MAC OS X上显示和隐藏文件和文件夹

Mac OS X 快速添加新文件

markdown Mac OS X Rails开发环境设置

markdown Mac OS X上的Docker Machine

Mac OS X下使用自动操作实现一键复制文件或文件夹路径