Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Error: Cannot find module 'request'

Tags:

node.js

npm

Ok so this is a common error and I HAVE read this node.js: cannot find module 'request'

The request module IS installed in my node_modules. My complete node app is

var r = require("request");


var s = r('http://www.foo.com/');


s.on('data',function(chunk){

    console.log(">>>Data>>> "+chunk);
});


s.on('end', function(){
    console.log(">>>Done!");
})

I run my app by simply calling

node app

But I keep getting the same error

What gives?

My directory structure is

app.js
node_modules
    request
        node_modules
            bl
            combined-stream
            form-data
            hawk
            mime-types
            tough-cookie

The complete error trace is

module.js:340
    throw err;
          ^
Error: Cannot find module 'request'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (/Users/foo/Documents/.../app.js:1:71)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
like image 671
Bachalo Avatar asked Feb 02 '15 01:02

Bachalo


People also ask

How do I resolve Cannot find module error using node JS?

To fix the Cannot find module error, simply install the missing modules using npm . This will install the project's dependencies into your project so that you can use them. Sometimes, this might still not resolve it for you. In this case, you'll want to just delete your node_modules folder and lock file ( package-lock.

Can not find module index JS?

If you are getting the "Cannot find module" error when trying to run a local file, make sure that the path you passed to the node command points to a file that exists. For example, if you run node src/index. js , make sure that the path src/index. js points to an existing file.


1 Answers

Something looks wrong in your directory structure. I would nuke the node_modules directory and redo the npm command.

It's always a good idea to maintain a package.json file and see the dependencies written out

cd getFoo
npm init # answer the qestions
npm install --save request
node app.js
like image 200
Samer Buna Avatar answered Oct 04 '22 09:10

Samer Buna