node.js: cannot find module 'request'

I had same problem, for me npm install request --save solved the problem. Hope it helps.


You should simply install request locally within your project.

Just cd to the folder containing your js file and run

npm install request

I have met the same problem as I install it globally, then I try to install it locally, and it work.


Go to directory of your project

mkdir TestProject
cd TestProject

Make this directory a root of your project (this will create a default package.json file)

npm init --yes

Install required npm module and save it as a project dependency (it will appear in package.json)

npm install request --save

Create a test.js file in project directory with code from package example

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body); // Print the google web page.
  }
});

Your project directory should look like this

TestProject/
- node_modules/
- package.json
- test.js

Now just run node inside your project directory

node test.js