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.
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 &
nvm and all seems to be working now!
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
Probably good to note some additional
nvm commands for future reference (from the docs):
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 install 8.0.0 Install a specific version number
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.
--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
So now I can just type
nvmi 12.16.1 (or whatever version) to get the latest Node and my global packages. Voila!
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 🙂
More big work to do on Chingu project asap.