How to update npm to the latest version when custom global directory is set

2.2k views Asked by At

Updating npm the usual way with

npm i npm@latest -g

leads to the latest version of npm installed in my custom directory .npm-global/ set via

prefix=${HOME}/.npm-global

in my .npmrc. However the old version /usr/local/bin/npm takes precendence. Attempting to naively link leads to error:

○ → link ~/.npm-global/bin/npm npm

dhcp-160 in /usr/local/bin ○ → npm module.js:471 throw err; ^

Error: Cannot find module '../lib/utils/unsupported.js'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at /usr/local/bin/npm:19:21
at Object.<anonymous> (/usr/local/bin/npm:79:3)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)

So how can I switch to the new npm version?

1

There are 1 answers

2
Sireesha Simakurthy On

Execute below commands in the same order:

sudo brew uninstall node
brew update
brew upgrade
brew cleanup
brew install node
sudo chown -R $(whoami) /usr/local
brew link --overwrite node
brew post install node

Reference Page: Node.js not linked error