Why the hell doesn’t PhantomJS work anymore! I’ve just reinstalled my modules …
I’ve been using PhantomJS on various projects in the past months. PhantomJS is definitely a useful tool but, more often than you’d like, it is related to weird build or test errors. Last time for me, I was unable to properly install my modules and even when npm gave me an OK after multiple attempts, test builds using PhantomJS would still crash.
The last symptoms to date
Just when I had other things to do than fix my build chain, I had to deal with that issue when running part of my tests :
At that point, I’m seeing “phantomjs” everywhere in my logs so I say to myself : “Ok, phantom’s install has gone wrong again somehow, let’s fix it, it’s simple”. Obviously, that did not quite go as planned, but the fix I found is actually pretty simple, honest =)
What I attempted
Like most problems where it seems a module was not isntalled properly, I basically went on and removed my “node_modules” directory and attempted a fresh install.
At that point, phantom would not install directly and it’d take me 2
s to have npm give me the OK. I though “meh … good enough for the moment”,
boy was I wrong =)
Tests using phantom would still not run, I attempted a second “delete node_modules and re-install everything” just in case and, as logic would have it, I had the same issues at install and testing.
At that point, I turned to one of the developer’s best friends (aka : StackOverflow) and, surprisingly, did not find much topics on that kind of issue. I tried globally installing phantom (yuck!), and it obviously did not do me any good. Presumably,when using phantomjs with other components like casper, the test runner sometimes mixes up which binary to run. I wasn’t convinced (the same tests ran fine on my laptop without any global install + it reminds me of those developers that install everything globally “just in case” and end up messing up their whole configuration) and … well … I’m still not =).
What seems to fix everything
One thing I forgot is that, as npm uses a cache, if I get a weird version of a package for some reason, it can stay in the cache even if I think I’m reinstalling everything. https://github.com/Medium/phantomjs/issues/435 brought me back on track.
In the end, I deleted my cache on top of removing my node_modules before re-installing :
… and PhantomJS did not complain anymore and I was able to run my tests again =)
When in doubt with install issues, never forget
before bothering with reinstalls.
I’ll try and make my CI delete that cache if the “build” steps of my apps fail from now on, so that it’ll, hopefuly, allow me to just re-run a build immediately =)
npm cache clean
Hope that helps ;)