I Broke Everything (nvm To The Rescue)

Daily Standup

In working on the Chingu project, I brilliantly managed to break node and npm on my computer. Awesome! So today I went through uninstalling everything and setting some things up as new.

Node, NPM , and nvm

I really don’t know how I managed to break npm; I was trying to use a new command npm outdated which is meant to produce a list of which packages are out of date in any given package-lock.json. It didn’t give me the expected result, but told me to update npm; so thinking that could solve the problem, I tried to do that with npm i -g npm and next thing I know all the node modules are gone and nothing is working.

Some research showed that by default sometimes npm can be installed without the ideal permissions, and this is what I think caused the problem. Instead you can use nvm to install node (and therefore npm) which avoids permission problems. There’s another fix which involves changing the folder permissions, but since it seems like there are other benefits to using nvm, I decided to go with that option.

So I took the leap and uninstalled Node all together, which deleted all of the npm global packages, npm, the whole lot. Then I re-installed Node & npm using nvm and all seems to be working now!

A Note About Global npm Packages

Unlike n which I used as a Node version manager before, nvm installs packages under each version of Node. That means if/when I install another version of Node on this machine, any global packages won’t be available to the new version. I’ll need to remember this flag for the packages to be brought into the new version:

nvm install node --reinstall-packages-from=node

NVM Commands

Probably good to note some additional nvm commands for future reference (from the docs):

Verify Installation

To verify that nvm is installed, do:

command -v nvm

which should output ‘nvm’ if the installation was successful. Please note that which nvm or nvm -v will not work, since nvm is a sourced shell function, not an executable binary. However nvm -v will print out a manual for using nvm.

Other Common Commands

nvm install 8.0.0              Install a specific version number
nvm use 8.0 Use the latest available 8.0.x release
nvm run 6.10.3 app.js Run app.js using node 6.10.3

What I’m Looking For

added August 2019, since I seem to always type the wrong thing!

$ nvm install --lts --reinstall-packages-from=node

Added February 19, 2020 since the above doesn’t work.

Using the --lts flag doesn’t work in the above…you have to write an actual version number to use the --reinstall-packages-from=node flag, which also must come at the end of the command. To avoid a lot of dumb copy/pasting, I added this function to my bash shell:

# install new node version
function nvmi {
nvm install $1 --reinstall-packages-from=node
}

So now I can just type nvmi 12.16.1 (or whatever version) to get the latest Node and my global packages. Voila!

Other Stuff

There is a lot on these days. It’s nice to have these projects to work on to make sure something fun happens every day 🙂

Up Next

More big work to do on Chingu project asap.