When using tools like n
or nvm
it's often a bit annoying to have the npm
version continually reset to whichever was bundled with the release being switched to.
Here's an alternative: having an npm installed separately.
This is especially useful if you want to try out npm@3
neatly.
sudo mkdir /opt/npm3
sudo chown $USER: /opt/npm3
npm install --global --prefix=/opt/npm3 npm@3
npm -v
/opt/npm3/bin/npm -v
This will install npm into /opt/npm3
, and make it accessible as long as you add /opt/npm/bin3
to your path.
You can do this with whichever directories you want to create more versions.
npm3() {
if [ "$1" = "off" ]; then
echo "Removing npm3 from path"
export PATH=`echo $PATH | sed 's/\/opt\/npm3\/bin://g'`
return
fi
echo "Adding npm3 to path"
export PATH=/opt/npm3/bin:$PATH
}