how to update npm on macOS

Solution 1:

This works on my mac.

Based on docs https://docs.npmjs.com/troubleshooting/try-the-latest-stable-version-of-npm :

npm install -g npm@latest

There is a note stated on the docs that depends on your installation method, you might addd some sudo.

Upgrading on *nix (OSX, Linux, etc.)

(You may need to prefix these commands with sudo, especially on Linux, or OS X if you installed Node using its default installer.)

Solution 2:

Perhaps you have already solved this, but here is what I found when I had exactly this issue. I had 2 versions of npm installed.

I verified this as follows:

grep \"version\" ~/.npm-packages/lib/node_modules/npm/package.json

  "version": "6.2.0"

grep \"version\" /usr/local/lib/node_modules/npm/package.json

  "version": "5.6.0",

I worked around the issue by updating the path in my bash profile, but would like to know why (how) I ended up with 2 versions. Here is the update:

tail -2 ~/.bash_profile

NPM_PACKAGES="${HOME}/.npm-packages"

PATH="$NPM_PACKAGES/bin:$PATH"