Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js: cannot find module 'request'

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.

like image 826
Sonic Soul Avatar asked May 10 '13 12:05

Sonic Soul


People also ask

How do I resolve Cannot find module error using node JS?

To solve the "Cannot find module" error in Node. js, make sure to install the package from the error message if it's a third party package, e.g. npm i somePackage . If you get the error with a local module, make sure to point the node command to a file that exists.

Can not find module in node JS?

To fix Cannot find module errors, install the modules properly by running a npm install command in the appropriate directory as your project's app. js or index. js file. or delete the node_modules folder and package-lock.

Can not find module npm?

To solve the error "Cannot find module 'express'", install the package by running the command npm install express in the root directory of your project. If you don't have a package. json file, create one by running npm init -y . The error occurs when we try to import the express package without installing it.

Is request deprecated?

As of February 11, 2020, one of the biggest NPM packages — Request — has been officially deprecated. This popular library has been around for more than a decade, with the first version released in 2009. Since then, it has received more than 16 million weekly downloads and more than 47,000 libraries are dependent on it.


1 Answers

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 
like image 130
glukki Avatar answered Sep 18 '22 08:09

glukki