Node NPM install fails with “Error: Cannot find module” – Solution

There are 187 thousand results on Google about this npm install error “Error: Cannot find module” and pretty much all responses say the say the same “delete your entire node installation.”

You might have an error like this:

> node install.js

module.js:328
throw err;
^

Error: Cannot find module 'readable-stream'
at Function.Module._resolveFilename (module.js:326:15)
at Function.Module._load (module.js:277:25)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object. (/usr/lib/node_modules/phantomjs-prebuilt/node_modules/extract-zip/node_modules/concat-stream/index.js:1:78)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
npm ERR! Linux 3.13.0-32-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "-g" "install" "[email protected]"
npm ERR! node v4.3.0
npm ERR! npm v2.14.12
npm ERR! code ELIFECYCLE

npm ERR! [email protected] install: `node install.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script 'node install.js'.
npm ERR! This is most likely a problem with the phantomjs-prebuilt package,
npm ERR! not with npm itself.

I took the time to actually troubleshoot the error and found that it comes to file and directory permissions – npm can install the dependent modules as root, change the permissions and then unable to open them again!

Solution: You can fix the issue by changing the directories and files in /usr/lib/node_modules to be allowed to be read by everybody on your system:

find /usr/lib/node_modules -type d | xargs chmod go+rx
find /usr/lib/node_modules -type f | xargs chmod go+r

Leave a Comment