my tale of npm woe – when all else fails, clear you cache!

Tonite was one of those “fun” times as a software developer. By fun, I mean one of those moments when you seriously think about having another profession due to hours of frustration trying to debug something, aaargh! My buddy Howard Dierking and I have a running joke that we’re going to open an ice cream shop which tonight sounded like a great idea.

If you have no patience to read my torturous experience, jump to the end of the post.

Back to my tale of woe. Tonight I started off with the intent of porting an app that I originally I wrote in .NET to Node. The app has both a web api and a jQuery front end. I was pretty excited about getting some hardcore coding time and even tweeted about the modules I was planning to use namely expressjs, socket.io, restify and mocha for my tests. So as I usually do, I created an empty folder, and then went and installed my modules. Everything worked grand until I got to install restify. When I did, it failed gloriously with the following hideous error.

What the??? OK, well that obviously did not work. So then I went and deleted my node_modules folder and installed again. Same error.

OK, well let me try installing a previous version. “npm install node-uuid@1.3.2″. Works! In the future I’ll know that was a big clue, unfortunately not this time. Instead I thought node-uuid _itself_ had a problem. OK, so next I went to the restify git repo and pulled down all the code. I then change the package.json for node-uiid to 1.3.2, then npm install restify and it works perfectly! Aha, it MUST be something wrong with node-uuid 1.3.3 right! I think so, so I head over to github and open an issue.

I then tweet about the issues I am having. Einar Otto (socket.io) graciously steps in to help me out. We’re on skype together trying to figure out just what is wrong. First thing he does on his side is “npm install restify” and what do you know it works. I then jump over to my mac book pro (my other mac) and do the same and it also works! Ah, so now we think it’s something possibly due to my machine which is an iMac from mid 2010, it could be Einar says because I am using the prebuilt version of node, I should build the source. I also start speculating completely crazy ideas, like maybe somehow it has to do with my username having an underscore. Yes folks, I became delirious.

And then hours later, just when I am about to accept defeat and switch off my nice 21″ imac to use my mbp, Einar says “Hmm, I wonder if maybe you should clear you cache”

What? Of course, npm has a local cache for modules that are installed so that it does not have to get them every time. Einar tells me where the cache is and I clear it. Then the moment of truth, “npm install restify”

That was it! Some module turd stuck in my cache!

Moral of the story, when something seems fishy with a module you are installing, you’ve tried everything and nothing else seems to work, clear you cache! It might save you from switching professions.

npm cache clean

Isaac if you are listening, it would be nice if there was some way npm detected repeated failures and prompted the user to clean the cache. That will save node devs a lot of frustration!

As of npm 5.x, if you want to clear the npm cache, you have to A. use npm cache clean instead of npm cache clear and B. use the –force flag as in:
npm cache clean –force

Daniel Arnost

Can someone tell me how to properly npm cache clean/clear on windows? Do I need to be in a certain directory? I am trying to use webpack for React ( I am a student). It was working fine, now webpack -w doesnt work on anything, and yields :
ERROR in ./app/app.js
Module build failed: SyntaxError: C:UserstestDesktoppackage.json: Error while parsing JSON – Unexpected end of JSON input **My teachers gave me and verified the files were error free**

It’s 2015 and this post is still incredibly relevant… Which is pretty tragic really! Thanks Glenn, was going crazy. “How come the CI works but it doesn’t on my machine?”

marcospgp

Thanks for the post

http://88pockets.com/ Marnus Weststrate

Be sure to use npm cache cleaN, and not npm cache cleaR. The latter fails silently, and doesn’t remedy the headache!

Mit

i found the similar errors, when installing socket.io. For me clearing the cache does not work. And also I’ve tried the different work-around mentioned in the above, but nothing seems like working for me. could someone help me to sort it out.

I use the below cmd to install socket.io

npm install –save socket.io

http://blogs.msdn.com/gblock Glenn Block

Glad to help!

Julian Harty

+1 from another fan. Thank you for enabling me to fix my system.

Travis Wimer

It’s been several years, and unfortunately this is still a problem. Thanks for helping me avoid wasting 5 hours!

http://buwilliams.tumblr.com/ Buddy Williams

Yeah, this happened to me. Sadly, I didn’t find this post quickly. I spent about 6 hours going insane. Therefore, sir if you are ever in town (Atlanta, GA) I shall bestow your favorite beverage.

http://blogs.msdn.com/gblock Glenn Block

Thanks everyone for the comments. I am happy to hear the post has proved useful!

http://markthethomas.github.io/ markthethomas

Found this to be a solution for more than a few npm errors I’ve encountered (and caused, to be fair)

fairmountfair

You are my favorite person on the internet right now. Thank you so much.

Jon de la Motte

Bless your heart my friend, this just saved my day.

Rohan Nicholls

Thank you so much. Just saved me giving up and going home in disgust.

http://thunki.com Nils N. Haukås

Fantastic post! I was also trudging through a land of woe when I thankfully stumbled upon your tip on clearing the npm cache.