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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With