5

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)
Community
  • 1
  • 1
Bachalo
  • 6,965
  • 27
  • 95
  • 189
  • 1
    Is the `node_modules` directory in the script's vicinity? Can you show me your directory structure? – Sven Feb 02 '15 at 01:31
  • see my directory structure above. My node version is v0.8.4 and npm 1.1.45 – Bachalo Feb 02 '15 at 02:30
  • I also ran npm init to create a package.json file. Is it possible I have a faulty node installation? – Bachalo Feb 02 '15 at 02:36
  • Does this answer your question? [node.js: cannot find module 'request'](https://stackoverflow.com/questions/16482600/node-js-cannot-find-module-request) – Michael Freidgeim Mar 20 '23 at 08:18

3 Answers3

18

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
Samer Buna
  • 8,821
  • 9
  • 38
  • 55
5

Go to the root folder of your app

cd my-app

Delete folder node_modules

rm -rf node_modules

Reinstall package.json

npm install

Start the server

npm start
drjorgepolanco
  • 7,479
  • 5
  • 46
  • 47
1

I had the same problem, and i installed the request package like that: "npm install request --save" and that solved the problem.