Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

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.

Building the source doesn’t work, switching to different folders doesn’t work, trying like 5 versions of node doesn’t work, nothing works!!!!!!!!

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!

This entry was posted in node.js. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • vipin singh

    This post saved me <3 .

  • obviously

    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**

    Also on my vast voyage to find out how to get it running again I keep seeing “darwin” in threads.
    $ npm install
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_moduleschokidarnode_modulesfsevents):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.0.15: wanted {“os”:”darwin”,”arch”:”any”} (current: {“os”:”win32″,”arch”:”x64″})
    npm WARN react-example@1.0.0 No description
    npm WARN react-example@1.0.0 No repository field.

  • http://www.codebetter.com/glennblock Glenn Block

    This reply is 4 years overdue, but glad it worked for you!

  • http://www.codebetter.com/glennblock Glenn Block

    Happy New Year! :-)

  • David Brower

    Helped me out too! 4th January 2016.

  • http://www.codebetter.com/glennblock Glenn Block

    Glad to help, but sorry this post is still needed :-(

  • John Reilly

    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. 

  • James Sun

    Thanks for your blog post. I, too, was experiencing weird behavior when trying to install a package via NPM.

  • http://twitter.com/fijiaaron Aaron Evans

    I’ve seen stuff like this.
    I’m looking for a way to have CI builds specify a separate npm cache for each build so it can avoid these sort of problems.

  • Glenn Block

    Kelsey I am sorry to hear that. I hope you didn’t spend as much time / have as much paind as I did.

  • Anonymous

    I had the same issue. :)