Not sure why, but whenever I change the node version using n, my global npm is reverted to an old version and I have to run npm install npm@latest -g every time I switch. Is there a way I can tell n to ignore whatever internal npm it is using?
npm is bundled with node. It will switch to whichever npm version was bundled with the node version you switch to.
Actually the issue as I see it is that it seems impossible to keep npm updated properly - the npm version is "locked" to the node version it was installed with!
For example if I install n stable I have npm version 1.4.28. I then update npm to 2.1.7. Then I switch to a different version of node and npm switches also. OK I get that... but if I switch back to n stable I am back at 1.4.28 again.
There must be some way to update npm and have it stick, right? Otherwise I can't use this amazingly handy utility. :(
node and npm seem to be going their separate ways. Here's how to decouple completely.
Now you can switch node versions with n all day and npm will remain stable as well as your global
modules. Cheers:
# save a list of what is installed globally
npm -g ls --depth=0 > npm_global_packages.txt
# Make a directory for global installations:
mkdir ~/.npm-global
# Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
# NOTE: add this to your profile as well:
export PATH=~/.npm-global/bin:$PATH
npm install npm -g
echo "NPM global directory changed: $(npm config get prefix)"
# Remove old npm
sudo rm /usr/local/bin/npm
sudo rm -rf /usr/local/lib/node_modules
# Now reinstall any global npm modules using the list we created above
Perhaps with the changes suggested in #212. Now would also be a good time to introduce a similar command such as n npm 2.1.7 as well?
Yep it would be great to be able to switch npm separately. Most of the time having to do npm i -g npm seems a bit cumbersome.
I think all we have to do is to tell n to only copy /lib/node_modules/npm from the downloaded Node.js package if it's not already installed, right?
What about npmbrew?
Oh yeah, at a first glance npmbrew does look pretty good.
Actually the issue as I see it is that it seems impossible to keep npm updated properly - the npm version is "locked" to the node version it was installed with!
I'm not convinced that using newer npm with older node will cause you any fewer problems than using older npm with newer node. The safest way to go is going to be to use the npm bundled with your node version, especially since whoever else is to npm install your thing likely is going to have the bundled version of npm, not your special version that you like.
I found it more annoying that "npm list -g" throws a lot of errors (invalid/extraneouse mostly) after I changed node-version thru n ...
Solutions ...
A) Update to latest npm:
npm update -g
B) Reinstall the existing npm-version:
npm install -g npm@VERSION
But why I have to do that?
Funny fact: As root "npm -v" always returns the version of the RPM i installed to install node/n ... for every other user "npm -v" works as expected ...
+1, because npm3 breaks some of my project, so I need to downgrade to npm2 every time I switch.
@cades Hmm, well, if you happen to be using fish-shell, check out fnm for a native solution. Else, you'll either have to use nvm or nave.
@bucaran thanks for suggestion! I'll give nave a try. But I'm still wondering why n can not do this? Is it possible to add this feature (even break the backward compatibility)?
Related https://github.com/tj/n/pull/364
This issue has not had any activity in over six months. It isn't likely to get acted on due to this report.
Feel free to open a new issue if it comes up again, with new information and renewed interest.
Thank you for your contributions.
For future visitors to this page, please note that there is now support for preserving the npm install when installing node. See --preserve and N_PRESERVE_NPM:
Most helpful comment
node and npm seem to be going their separate ways. Here's how to decouple completely.
Now you can switch node versions with n all day and npm will remain stable as well as your global
modules. Cheers: