NodeJS + NPM “Cannot find module”

TL;DR: install your NPM packages globally. Like this (where “homebridge” is whatever package you are installing):

sudo npm install homebridge -g

This was driving me crazy. I have been trying to install a couple of NPM packages with dependencies, and run them at startup as a service on an Ubuntu server. I knew the packages weren’t ending up in the right place (even when run as sudo), but I couldn’t figure out why.

Using the -g (global) option cleared everything back up. Now all the dependencies are installed for all users, and my service starts without errors.

The errors were like these:

Error: Cannot find module 'request'

4 verbose stack Error: Cannot find module 'internal/fs'
4 verbose stack     at Function.Module._resolveFilename (module.js:472:15)
4 verbose stack     at Function.Module._load (module.js:420:25)
4 verbose stack     at Module.require (module.js:500:17)
4 verbose stack     at require (internal/module.js:20:19)
4 verbose stack     at evalmachine.:17:20
4 verbose stack     at Object. (/usr/local/lib/node_modules/npm/node_modules/cmd-shim/node_modules/graceful-fs/fs.js:11:1)
4 verbose stack     at Module._compile (module.js:573:32)
4 verbose stack     at Object.Module._extensions..js (module.js:582:10)
4 verbose stack     at Module.load (module.js:490:32)
4 verbose stack     at tryModuleLoad (module.js:449:12)
npm ERR! Problems were encountered
npm ERR! Please correct and try again.
npm ERR! missing: async@~0.9.0, required by FillinforMe@0.4.0
npm ERR! invalid: have readable-stream@2.0.5 (expected: latest) /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/connect-mongo/node_modules/mongodb/node_modules/readable-stream
npm ERR! extraneous: readable-stream@2.0.5 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/connect-mongo/node_modules/mongodb/node_modules/readable-stream
npm ERR! extraneous: grunt-karma@0.8.3 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/grunt-karma
npm ERR! extraneous: grunt-mocha-test@0.10.2 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/grunt-mocha-test
npm ERR! extraneous: karma@0.12.36 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/karma
npm ERR! extraneous: karma-chrome-launcher@0.1.12 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/karma-chrome-launcher
npm ERR! extraneous: karma-coffee-preprocessor@0.2.1 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/karma-coffee-preprocessor
npm ERR! extraneous: karma-coverage@0.2.7 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/karma-coverage
npm ERR! extraneous: karma-firefox-launcher@0.1.6 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/karma-firefox-launcher
npm ERR! extraneous: karma-html2js-preprocessor@0.1.0 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/karma-html2js-preprocessor
npm ERR! extraneous: karma-jasmine@0.1.5 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/karma-jasmine
npm ERR! extraneous: karma-ng-html2js-preprocessor@0.1.2 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/karma-ng-html2js-preprocessor
npm ERR! extraneous: karma-ng-scenario@0.1.0 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/karma-ng-scenario
npm ERR! extraneous: karma-phantomjs-launcher@0.1.4 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/karma-phantomjs-launcher
npm ERR! extraneous: karma-requirejs@0.2.2 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/karma-requirejs
npm ERR! extraneous: karma-script-launcher@0.1.0 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/karma-script-launcher
npm ERR! invalid: have request@2.67.0 (expected: latest) /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/meanio/node_modules/request
npm ERR! extraneous: request@2.67.0 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/meanio/node_modules/request
npm ERR! invalid: have readable-stream@2.0.5 (expected: latest) /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/mongoose/node_modules/mongodb/node_modules/readable-stream
npm ERR! extraneous: readable-stream@2.0.5 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/mongoose/node_modules/mongodb/node_modules/readable-stream
npm ERR! extraneous: requirejs@2.1.18 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/requirejs
npm ERR! extraneous: should@3.3.1 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/should
npm ERR! extraneous: supertest@0.11.0 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/supertest
npm ERR! Error: Problems were encountered
npm ERR! Please correct and try again.
npm ERR! missing: async@~0.9.0, required by FillinforMe@0.4.0
npm ERR! invalid: hawk@3.1.2 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/hawk
npm ERR! invalid: forever-agent@0.6.1 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/forever-agent
npm ERR! invalid: http-signature@1.1.0 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/http-signature
npm ERR! extraneous: jsbn@0.1.0 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/jsbn
npm ERR! invalid: oauth-sign@0.8.0 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/oauth-sign
npm ERR! invalid: qs@5.2.0 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/qs
npm ERR! invalid: tunnel-agent@0.4.2 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/tunnel-agent
npm ERR! invalid: form-data@1.0.0-rc3 /Users/tdiddy/herokusupport/damp-mesa-56191/node_modules/form-data
npm ERR!     at shrinkwrap_ (/usr/local/lib/node_modules/npm/lib/shrinkwrap.js:29:15)
npm ERR!     at /usr/local/lib/node_modules/npm/lib/shrinkwrap.js:23:5
npm ERR!     at /usr/local/lib/node_modules/npm/lib/ls.js:44:30
npm ERR!     at /usr/local/lib/node_modules/npm/node_modules/read-installed/read-installed.js:116:5
npm ERR!     at /usr/local/lib/node_modules/npm/node_modules/read-installed/read-installed.js:238:14
npm ERR!     at cb (/usr/local/lib/node_modules/npm/node_modules/slide/lib/async-map.js:48:11)
npm ERR!     at /usr/local/lib/node_modules/npm/node_modules/read-installed/read-installed.js:238:14
npm ERR!     at cb (/usr/local/lib/node_modules/npm/node_modules/slide/lib/async-map.js:48:11)
npm ERR!     at /usr/local/lib/node_modules/npm/node_modules/read-installed/read-installed.js:238:14
npm ERR!     at cb (/usr/local/lib/node_modules/npm/node_modules/slide/lib/async-map.js:48:11)

Leave a Reply

Your email address will not be published. Required fields are marked *