After pulling down a module from GitHub and following the instructions to build it, I try pulling it into an existing project using:
> npm install ../faye
This appears to do the trick:
> npm list
/home/dave/src/server
└─┬ faye@0.7.1
├── cookiejar@1.3.0
├── hiredis@0.1.13
└── redis@0.7.1
But Node.js can't find the module:
> node app.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'faye'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Object.<anonymous> (/home/dave/src/server/app.js:2:12)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Array.0 (module.js:470:10)
I really want to understand what is going on here, but I'm at a bit of a loss as to where to look next. Any suggestions?
Removing node/npm and then re-installing the stable(not the latest) version worked for me.
This error can be encountered if you are
require
ing a module that has a missing or incorrectmain
field in its package.json. Though the module itself is installed, npm/node has to use a single .js file as an entrypoint to your module. If themain
field is not there, it defaults to looking forindex.js
in your module's folder. If your module's main file is not called index.js, it won't be able torequire
it.Discovered while turning a
browserify
-based module into a CommonJSrequire
-able module;browserify
didn't care about the missingmain
field, and so the error had gone unnoticed.Change the directory and point to your current project folder and then "npm install". .
This will install all dependencies and modules into your project folder.
I faced the same problem when someone else in the team updated
package.json
in SVN. Merely removing thenode_modules
directory did not help. How I solved the problem is:Hope this helps someone!
PRO TIP:
This error happened to me, while fighting fatigue and mild illness, because I typed
node blah
instead ofnpm blah
.The error message received wasn't angry enough to alert me to my own folly!
Using
npm install
installs the module into the current directory only (in a subdirectory callednode_modules
). Is app.js located underhome/dave/src/server/
? If not and you want to use the module from any directory, you need to install it globally usingnpm install -g
.I usually install most packages locally so that they get checked in along with my project code.
Update (8/2019):
Nowadays you can use package-lock.json file, which is automatically generated when npm modifies your node_modules directory. Therefore you can leave out checking in packages, because the
package-lock.json
tracks the exact versions of your node_modules, you're currently using. To install packages frompackage-lock.json
instead ofpackage.json
use the commandnpm ci
.Update (3/2016):
I've received a lot of flak for my response, specifically that I check in the packages that my code depends on. A few days ago, somebody unpublished all of their packages (https://medium.com/@azerbike/i-ve-just-liberated-my-modules-9045c06be67c#.kq9s64clp) which broke React, Babel, and just about everything else. Hopefully it's clear now that if you have production code, you can't rely on NPM actually maintaining your dependencies for you.