node.js: cannot find module 'request'

214,289

Solution 1

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

Solution 2

You should simply install request locally within your project.

Just cd to the folder containing your js file and run

npm install request

Solution 3

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

Solution 4

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

Solution 5

if some module you cant find, try with Static URI, for example:

var Mustache = require("/media/fabio/Datos/Express/2_required_a_module/node_modules/mustache/mustache.js");

This example, run on Ubuntu Gnome 16.04 of 64 bits, node -v: v4.2.6, npm: 3.5.2 Refer to: Blog of Ben Nadel

Share:
214,289

Related videos on Youtube

Sonic Soul
Author by

Sonic Soul

Updated on April 24, 2020

Comments

  • Sonic Soul
    Sonic Soul about 4 years

    I installed request module, and getting the error:

    module.js:340
        throw err;
              ^
    Error: Cannot find module 'request'
    

    i've read all the posts about this error, and understand that this is because module requests is not globally found, but i've already tried the 2 suggestions

    npm install request -g

    should this install it in /usr/loca/bin ? because i don't see it there.

    and

    sudo npm link

    /usr/local/lib/node_modules/request -> /Users/soulsonic/dev/sandbox/node_test/request

    i restarted terminal after each command, but keep getting the cannot find module error.

    update

    there must have been some sort of conflict in my initial directory, because "npm install request" was not adding "request" under node_modules (there 10 others in there) .. after switching to a new directory it just worked.

    if i run it with -g switch, i do see it bing installed to /usr/local/lib/node_modules/request.

    it seems that i just need to update my profile so that above path is automatically added.

  • Sonic Soul
    Sonic Soul about 11 years
    after "npm install request" i did copy my app.js to the request folder and tried running from there with the same result. and i will need to use it from more than just one project.
  • SLaks
    SLaks about 11 years
    @SonicSoul: You're misunderstanding how node packages work. require('request') looks in ./node_modules/request, and that's where npm install puts it. You should run npm install from the root directory of each app, and you should never touch the node_modules folder yourself.
  • Noah
    Noah about 11 years
    before running npm install request, you should run npm init. Follow the prompts which will create a package.json in your project folder. Then run npm install -S request which will both install the request module into the node_modules folder as well as add request to your package.json file
  • Sonic Soul
    Sonic Soul about 11 years
    yeah that's exactly the example i've followed. and i run the npm installer 10 times now.. in my test project and using -g flag. i can see node_modules in my test directory, but request is not in there.. it continues to give me that error..
  • Drumnbass
    Drumnbass about 8 years
    @SLaks doest it mean that everytime I install any package I need to enter in its root folder and execute npm install? for all of them?
  • SLaks
    SLaks about 8 years
    @Drumnbass: No. npm install installs all dependencies too.
  • Sonic Soul
    Sonic Soul almost 8 years
    because even though I had some weird conflict, these are the right steps to follow. it worked once I did it again in a new directory. I added an update on what happened to me which will probably be rare for others
  • Arya
    Arya almost 6 years
    Mine worked after this workaround. I was previously installing in with '--global'. Thanks.
  • Nekto
    Nekto about 5 years
    @SLaks Exactly what I needed :) Thank you sir!