Skip to content

Instantly share code, notes, and snippets.

@vinayakkulkarni
Forked from DanHerbert/fix-homebrew-npm.md
Created May 26, 2018 16:27
Show Gist options
  • Save vinayakkulkarni/7b3ff79494bddb15b94239a6adacdf27 to your computer and use it in GitHub Desktop.
Save vinayakkulkarni/7b3ff79494bddb15b94239a6adacdf27 to your computer and use it in GitHub Desktop.

Revisions

  1. @DanHerbert DanHerbert revised this gist Sep 22, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions fix-homebrew-npm.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ This solution fixes the error caused by trying to run `npm update npm -g`. Once

    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 right defaults, install npm as its own pacakge, and configure the location for global npm modules to be installed.
    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
    @@ -18,7 +18,7 @@ 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 commands you install globally are usable. To do this, add the following line to your `~/.bash_profile`:
    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"
  2. @DanHerbert DanHerbert revised this gist Sep 22, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fix-homebrew-npm.md
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ 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 `~/.node/bin` to your `PATH` so commands you install globally are usable. I added this line to my `~/.path` script, which gets run via `~/.bash_profile`.
    Node and npm should be correctly installed at this point. The final step is to add `~/.npm-packages/bin` to your `PATH` so commands you install globally are usable. To do this, add the following line to your `~/.bash_profile`:

    ```bash
    export PATH="$HOME/.npm-packages/bin:$PATH"
  3. @DanHerbert DanHerbert revised this gist Sep 22, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fix-homebrew-npm.md
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ 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 `~/.node/bin` to your `PATH` so commands you install globally are usable. I added this line to my `~/.path` script, which gets run via `~/.bash_profile`.

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

    Now you can re-install any global npm packages you need without any problems.
  4. @DanHerbert DanHerbert revised this gist Feb 13, 2016. 1 changed file with 25 additions and 28 deletions.
    53 changes: 25 additions & 28 deletions fix-homebrew-npm.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,30 @@
    # Fixing npm On Mac OS X for Homebrew Users

    If you just want to fix the issue quickly, scroll down to the "solution" section below.
    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 right defaults, install npm as its own pacakge, and configure the location for global npm modules to be installed.

    ```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 `~/.node/bin` to your `PATH` so commands you install globally are usable. I added this line to my `~/.path` script, which gets run via `~/.bash_profile`.

    ```bash
    export PATH="$HOME/.node/bin:$PATH"
    ```

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

    ## Explanation of the issue

    @@ -40,30 +64,3 @@ There's an [NPM bug for this exact problem](https://github.com/npm/npm/issues/37

    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.

    ## 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 right defaults, install npm as its own pacakge, and configure the location for global npm modules to be installed.

    ```bash
    rm -rf /usr/local/lib/node_modules
    brew uninstall node
    brew install node --without-npm
    echo prefix=~/.node >> ~/.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 `~/.node/bin` to your `PATH` so commands you install globally are usable. I added this line to my `~/.path` script, which gets run via `~/.bash_profile`.

    ```bash
    export PATH="$HOME/.node/bin:$PATH"
    ```

    Now you can re-install any global packages with the command below, replacing the npm modules with whatever global packages you need.

    ```bash
    npm install -g http-server node-inspector forever nodemon
    ```
  5. @DanHerbert DanHerbert revised this gist Apr 18, 2015. 1 changed file with 9 additions and 5 deletions.
    14 changes: 9 additions & 5 deletions fix-homebrew-npm.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ If you just want to fix the issue quickly, scroll down to the "solution" section

    ## Explanation of the issue

    If you're a Mac 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 will see an error like this:
    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
    @@ -36,20 +36,24 @@ npm ERR! not ok code 0
    ```

    There's an [NPM bug open for this exact problem](https://github.com/npm/npm/issues/3794) and a long thread that is still actively being debated, but the summary is that `npm` is its own package manager and it is therefore better to have `npm` manage itself and its packages instead of letting Homebrew do it. Also, using the Homebrew version of `npm` requires `sudo` to install global packages. That's also a very bad idea.
    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.

    ## 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 right defaults, install npm as its own pacakge, and configure the location for global npm modules to be installed.

    ```bash
    rm -rf /usr/local/lib/node_modules
    brew uninstall node
    brew install node --without-npm
    echo prefix=~/.node >> ~/.npmrc
    curl -L https://www.npmjs.org/install.sh | sh
    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 `~/.node/bin` to your `PATH` so commands you install globally are usable. I added this line to my `~/.path` script, which gets run via `~/.bash_profile`.
    @@ -58,8 +62,8 @@ Node and npm should be correctly installed at this point. The final step is to a
    export PATH="$HOME/.node/bin:$PATH"
    ```

    You'll need to re-install your global node modules again. As an example, a few I use are:
    Now you can re-install any global packages with the command below, replacing the npm modules with whatever global packages you need.

    ```bash
    npm install -g grunt-cli http-server uglify-js jshint yo node-inspector forever nodemon
    npm install -g http-server node-inspector forever nodemon
    ```
  6. @DanHerbert DanHerbert revised this gist Aug 5, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions fix-homebrew-npm.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ If you just want to fix the issue quickly, scroll down to the "solution" section

    ## Explanation of the issue

    If you're a Mac 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 try to do `npm update npm -g`, you will see error like this:
    If you're a Mac 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 will see an error like this:

    ```
    $ npm update npm -g
    @@ -52,7 +52,7 @@ echo prefix=~/.node >> ~/.npmrc
    curl -L https://www.npmjs.org/install.sh | sh
    ```

    Node and npm should be correctly installed at this point. The final step is to add `~/.node/bin` to your `PATH` so commands you install globally are usable. I added this line to my `~/.path` script, which gets called via `~/.bash_profile`. If you want to see a full example you can check out [my dotfiles repo on github](https://github.com/DanHerbert/dotfiles).
    Node and npm should be correctly installed at this point. The final step is to add `~/.node/bin` to your `PATH` so commands you install globally are usable. I added this line to my `~/.path` script, which gets run via `~/.bash_profile`.

    ```bash
    export PATH="$HOME/.node/bin:$PATH"
  7. @DanHerbert DanHerbert revised this gist Mar 23, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fix-homebrew-npm.md
    Original file line number Diff line number Diff line change
    @@ -61,5 +61,5 @@ export PATH="$HOME/.node/bin:$PATH"
    You'll need to re-install your global node modules again. As an example, a few I use are:

    ```bash
    npm install -g grunt-cli http-server uglify-js jshint yo nodemon
    npm install -g grunt-cli http-server uglify-js jshint yo node-inspector forever nodemon
    ```
  8. @DanHerbert DanHerbert revised this gist Mar 13, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions fix-homebrew-npm.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    # Fixing npm On Mac OS X for Homebrew Users

    If you just want to fix the issue quickly, scroll down to the "solution" section below.

    ## Explanation of the issue

    If you're a Mac 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 try to do `npm update npm -g`, you will see error like this:
    @@ -56,8 +58,8 @@ Node and npm should be correctly installed at this point. The final step is to a
    export PATH="$HOME/.node/bin:$PATH"
    ```

    Now you should re-install any global node packages again. As an example, these are the ones I know I'll use:
    You'll need to re-install your global node modules again. As an example, a few I use are:

    ```bash
    npm install -g grunt-cli http-server uglify-js jshint bower yo nodemon
    npm install -g grunt-cli http-server uglify-js jshint yo nodemon
    ```
  9. @DanHerbert DanHerbert revised this gist Mar 13, 2014. 1 changed file with 2 additions and 7 deletions.
    9 changes: 2 additions & 7 deletions fix-homebrew-npm.md
    Original file line number Diff line number Diff line change
    @@ -40,15 +40,10 @@ There's an [NPM bug open for this exact problem](https://github.com/npm/npm/issu

    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.

    First, remove globally installed node modules. This step may not be necessary, but I do it to ensure my machine is clean before continuing. **This step will be very specific to your setup. I've provided the one I ran as an example only.** All other steps are completely copy/paste verbatim after this one.

    ```bash
    sudo npm uninstall -g grunt-cli yo uglify-js nodemon jshint http-server bower
    ```

    Next, run the following commands to remove node, re-install it with the right defaults, install npm as its own module, and configure the location for global npm modules to go.
    Run the following commands to remove all existing global npm modules, uninstall node & npm, re-install node with the right defaults, install npm as its own pacakge, and configure the location for global npm modules to be installed.

    ```bash
    rm -rf /usr/local/lib/node_modules
    brew uninstall node
    brew install node --without-npm
    echo prefix=~/.node >> ~/.npmrc
  10. @DanHerbert DanHerbert revised this gist Mar 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fix-homebrew-npm.md
    Original file line number Diff line number Diff line change
    @@ -55,7 +55,7 @@ echo prefix=~/.node >> ~/.npmrc
    curl -L https://www.npmjs.org/install.sh | sh
    ```

    Node and npm should be correctly installed at this point. The final step is to add `~/.node/bin` to your `PATH` so commands you install globally are usable. I added this line to my `~/.path`, which gets called via `~/.bash_profile`. If you want to see a full example you can check out [my dotfiles repo on github](https://github.com/DanHerbert/dotfiles).
    Node and npm should be correctly installed at this point. The final step is to add `~/.node/bin` to your `PATH` so commands you install globally are usable. I added this line to my `~/.path` script, which gets called via `~/.bash_profile`. If you want to see a full example you can check out [my dotfiles repo on github](https://github.com/DanHerbert/dotfiles).

    ```bash
    export PATH="$HOME/.node/bin:$PATH"
  11. @DanHerbert DanHerbert revised this gist Mar 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fix-homebrew-npm.md
    Original file line number Diff line number Diff line change
    @@ -55,7 +55,7 @@ echo prefix=~/.node >> ~/.npmrc
    curl -L https://www.npmjs.org/install.sh | sh
    ```

    Node and npm should be correctly installed at this point. The final step is to add `~/.node/bin` to your `PATH` so commands you install globally are usable.
    Node and npm should be correctly installed at this point. The final step is to add `~/.node/bin` to your `PATH` so commands you install globally are usable. I added this line to my `~/.path`, which gets called via `~/.bash_profile`. If you want to see a full example you can check out [my dotfiles repo on github](https://github.com/DanHerbert/dotfiles).

    ```bash
    export PATH="$HOME/.node/bin:$PATH"
  12. @DanHerbert DanHerbert revised this gist Mar 13, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion fix-homebrew-npm.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # Fixing npm On Mac OS X for Homebrew USers
    # Fixing npm On Mac OS X for Homebrew Users

    ## Explanation of the issue

    If you're a Mac 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 try to do `npm update npm -g`, you will see error like this:
  13. @DanHerbert DanHerbert renamed this gist Mar 13, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions instructions.md → fix-homebrew-npm.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # Fixing npm On Mac OS X for Homebrew USers
    ## Explanation of the issue

    If you're a Mac 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 try to do `npm update npm -g`, you will see error like this:
  14. @DanHerbert DanHerbert created this gist Mar 13, 2014.
    66 changes: 66 additions & 0 deletions instructions.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    ## Explanation of the issue

    If you're a Mac 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 try to do `npm update npm -g`, you will see 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 [email protected] { [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 open for this exact problem](https://github.com/npm/npm/issues/3794) and a long thread that is still actively being debated, but the summary is that `npm` is its own package manager and it is therefore better to have `npm` manage itself and its packages instead of letting Homebrew do it. Also, using the Homebrew version of `npm` requires `sudo` to install global packages. That's also a very bad idea.

    ## 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.

    First, remove globally installed node modules. This step may not be necessary, but I do it to ensure my machine is clean before continuing. **This step will be very specific to your setup. I've provided the one I ran as an example only.** All other steps are completely copy/paste verbatim after this one.

    ```bash
    sudo npm uninstall -g grunt-cli yo uglify-js nodemon jshint http-server bower
    ```

    Next, run the following commands to remove node, re-install it with the right defaults, install npm as its own module, and configure the location for global npm modules to go.

    ```bash
    brew uninstall node
    brew install node --without-npm
    echo prefix=~/.node >> ~/.npmrc
    curl -L https://www.npmjs.org/install.sh | sh
    ```

    Node and npm should be correctly installed at this point. The final step is to add `~/.node/bin` to your `PATH` so commands you install globally are usable.

    ```bash
    export PATH="$HOME/.node/bin:$PATH"
    ```

    Now you should re-install any global node packages again. As an example, these are the ones I know I'll use:

    ```bash
    npm install -g grunt-cli http-server uglify-js jshint bower yo nodemon
    ```