Error: Cannot find module 'node-fetch' in Express NodeJs
Hello guys, I'm starting to learn Nodejs two weeks ago. And I have cloned a small project of my teacher on Github to my local computer.
It's a mall project about Nodejs, that helps pull data from an API and display data to the browser. Like this:
index.js file
const fetch = require('node-fetch');
var express = require('express');
var app = express();
app.get('/', async (req, res) => {
const result = await getUser();
console.log(result);
});
var server = app.listen(5000, function () {
console.log('Server is running..');
});
async function getUser() {
try {
const response = await fetch('https://randomuser.me/api/');
if (!response.ok) {
throw new Error(`Error! status: ${response.status}`);
}
const result = await response.json();
return result;
} catch (err) {
console.log(err);
}
}
After building the source code, I ran node index.js
command I see an issue : Error: Cannot find module 'node-fetch'.
Error: Cannot find module 'node-fetch'
Require stack:
- C:\Users\conta\source\repos\NodeJs\Demo1\index.js
←[90m at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:841:27)←[39m
←[90m at Module.require (internal/modules/cjs/loader.js:1025:19)←[39m
←[90m at require (internal/modules/cjs/helpers.js:72:18)←[39m
at Object.<anonymous> (C:\Users\conta\source\repos\NodeJs\Demo1\index.js:1:15)
←[90m at Module._compile (internal/modules/cjs/loader.js:1137:30)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:985:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:878:14)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)←[39m {
code: ←[32m'MODULE_NOT_FOUND'←[39m,
Anyone can help me?
Thank you in advance.
-
N1
Nam Vo Jun 25 2022
I think you pull from Github, this project is already installed
node-fetch
package. You can openpackage.json
file and check it. If right, you can run the command below:npm update
It will auto-update all packages for you.
If not, you can install
node-fetch
package by using the command:npm install node-fetch
Hope this answer will useful for you.
-
M0
Mario Guzmán Jun 25 2022
To install node-fetch, you can run the command:
npm install node-fetch
Sometimes you need to clear and install npm first by using the command:
npm install -g npm npm cache clean npm update
It worked for me.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.