node.js: cannot find module 'request'
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
Related videos on Youtube
Sonic Soul
Updated on April 24, 2020Comments
-
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 about 11 yearsafter "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 about 11 years@SonicSoul: You're misunderstanding how node packages work.
require('request')
looks in./node_modules/request
, and that's wherenpm install
puts it. You should runnpm install
from the root directory of each app, and you should never touch thenode_modules
folder yourself. -
Noah about 11 yearsbefore running
npm install request
, you should runnpm init
. Follow the prompts which will create apackage.json
in your project folder. Then runnpm 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 about 11 yearsyeah 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 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 about 8 years@Drumnbass: No.
npm install
installs all dependencies too. -
Sonic Soul almost 8 yearsbecause 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 almost 6 yearsMine worked after this workaround. I was previously installing in with '--global'. Thanks.
-
Nekto about 5 years@SLaks Exactly what I needed :) Thank you sir!