Search code examples
node.jsfaker.js

cannot find module faker after npm install --save-dev


I want to install all my modules locally so I am installing everything with the "--save-dev" switch which updates the package.json.

I am trying to include this module so I installed using this command:

npm install Faker --save-dev

My app structure is like this:

app controllers models node_modules Faker server.js

So Faker is in the right place but when I add this code in my server.js file:

var faker = require('./Faker');

I get the following error message:

Error: Cannot find module './Faker'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/paulcowan/projects/async-talk/server.js:23:13)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10) 

But this works:

var Faker = require('./node_modules/Faker');

I did not think that I would have to include the node_modules part.


Solution

  • To get your require to work, you need do:

    var Faker = require('Faker');
    

    Any package installed by npm is required by name. Only modules that are required locally need a path-like require. Your require(./Faker); means "require a module from the same directory as this file, called 'Faker'".