There may be a problem with a package you are using, not npm itself. Sigh. Deep breath. Grunt. Okay – time to wipe the node_modules and start again.
Reasons aside, for now, there are a few commands I seem to run almost daily to try to recover from errors. Kelly Vaughn got me thinking this morning about this.
if at first you don’t succeed, try rm -rf node_modules && npm install— Kelly Vaughn 🐞 (@kvlly) April 22, 2019
There are a few contributing factors to me running into issues with npm packages.
I manage a lot of code, including some just infrequent enough where the packages get stale.
I work in coffee shops and poor WiFi areas quite often
Murphy’s Law surrounds me
So what do I do when I see one of the issues with my npm packages? Especially the ones that claim there may be something wrong with one of the packages and not npm itself? I clear the node_modules folder and try again.
rm -rf node_modules package-lock.json
And to do it with flair, I created a bash alias to run these commands.
Why run it in two commands? Mostly because I like to see that the first one completed then go run npm install manually. I have no good reason. You may prefer to run it all at once.
Here is how I set up my bash alias on my macOS.
alias npm-wipe=”rm -rf node_modules package-lock.json"
Hope this helps you out of a bind once in a while, too! Do you use this technique? If not, what do you do when npm errors bite you?