105

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.

Community
  • 1
  • 1
Sonic Soul
  • 23,855
  • 37
  • 130
  • 196

9 Answers9

203

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
glukki
  • 2,656
  • 1
  • 18
  • 20
  • 1
    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.. – Sonic Soul May 11 '13 at 16:38
  • 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 – Sonic Soul May 26 '16 at 12:51
39

You should simply install request locally within your project.

Just cd to the folder containing your js file and run

npm install request
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 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. – Sonic Soul May 10 '13 at 12:54
  • @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. – SLaks May 10 '13 at 13:09
  • 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 – Noah May 10 '13 at 17:51
  • @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? – Drumnbass May 08 '16 at 11:17
  • @Drumnbass: No. `npm install` installs all dependencies too. – SLaks May 08 '16 at 13:38
  • Mine worked after this workaround. I was previously installing in with '--global'. Thanks. – Arya Jun 07 '18 at 06:33
  • @SLaks Exactly what I needed :) Thank you sir! – Nekto Feb 16 '19 at 00:09
21

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

2

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

1

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

1

I tried installing the module locally with version and it worked!!

npm install request@^2.*

Thanks.

shamnas cv
  • 11
  • 3
0

I was running into the same problem, here is how I got it working..

open terminal:

mkdir testExpress
cd testExpress
npm install request

or

sudo npm install -g request // If you would like to globally install.

now don't use

node app.js or node test.js, you will run into this problem doing so. You can also print the problem that is being cause by using this command.. "node -p app.js"

The above command to start nodeJs has been deprecated. Instead use

npm start

You should see this..

testExpress@0.0.0 start /Users/{username}/testExpress
node ./bin/www

Open your web browser and check for localhost:3000

You should see Express install (Welcome to Express)

AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
0

ReferenceError: Can't find variable: require.

You have installed "npm", you can run as normal the script to a "localhost" "127.0.0.1".

When you use the http.clientRequest() with "options" in a "npm" you need to install "RequireJS" inside of the module.

A module is any file or directory in the node_modules directory that can be loaded by the Node. Install "RequiereJS" for to make work the http.clientRequest(options).

0

I ran into the same problem while installing the request globally on ubuntu.

$ npm install request --global

This solution works when installing request globally:

Setting the env variable NODE_PATH='/absolute/path/to/node/modules resolved this error.

$ export NODE_PATH=/usr/local/lib/node_modules

However, this will require you to export this variable for each shell session. A more permanent option is to:

  • add this to your export NODE_PATH=/usr/local/lib/node_modules .bashrc file, then
  • run source ~/.bashrcto execute the update .bashrc file

Note that the absolute path to globally installed node modules might differ in some cases. For instance, it may be /usr/lib/node_modules instead of /usr/local/lib/node_modules. Double-check the right path before setting the NODE_PATH

Pudding
  • 1
  • 1