How do I resolve "Cannot find module" error using Node.js?


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
└─┬ [email protected]
  ├── [email protected]
  ├── [email protected]
  └── [email protected]

But Node.js can't find the module:

> node app.js
        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?

10/25/2016 3:24:42 PM

Accepted Answer

Using npm install installs the module into the current directory only (in a subdirectory called node_modules). Is app.js located under home/dave/src/server/? If not and you want to use the module from any directory, you need to install it globally using npm 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 from package-lock.json instead of package.json use the command npm 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 ( 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.

3/17/2020 1:34:44 AM

npm install --save module_name

For example, if the error is:

{ [Error: Cannot find module '/root/.npm/form-data'] code: 'MODULE_NOT_FOUND' }

then you can resolve this issue by executing the command npm install --save form-data.


For TypeScript users, if you are importing a built-in Node module (such as http, path or url) and you are getting an error such as "Cannot find module "x" then the error can be fixed by running

npm install @types/node --save-dev

The command will import the NodeJS TypeScript definitions into your project, allowing you to use Node's built-in modules.


This happens when a first npm install has crashed for some reason (SIGINT of npm), or that the delay was too long, or data is corrupted. Trying an npm install again won't save the problem.

Something got wrong on the npm first check, so the best choice is to remove the file and to restart npm install.


If you use nvm, check that existing node_modules that are bindings to other libraries are compiled for the correct Node.js version.

I was having the same error. The reason was the following: We use nvm since we're running two apps on a server, one requires Node.js 5.6 because it uses node-gd (which doesn't run on Node.js 6 for now), the other requires Node.js 6. Node.js 6 is the apt-get installation.

Also we use the pm2 tool to deploy.

So, the default setup is that the pm2 process starts when nvm is not in effect, so it uses the apt-get installation of Node.js (version 6). So the main pm2 daemon starts with Node.js 6. If I run applications in fork mode they start in separate processes and nvm settings are in effect. When I run applications in cluster mode - they inherit the non-nvm environment.

So when I tried to switch to the cluster mode the application failed to start because the bindings compiled for 5.6 fail with this message.

I've fixed that by restarting pm2 when nvm setings are in effect. Also startup scripts should be fixed.


I experienced this error yesterday. Took me a while to realise that the main entry in package.json was pointing to a file that I'd moved. Once I updated that the error disappeared and the package worked.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]