. Advertisement .
..3..
. Advertisement .
..4..
Node.js is an extremely powerful JavaScript platform used to develop online chat applications, live video sites, single-page applications, and many other web applications. In the process of using will not be avoided from any system error. If you have a problem with this ReferenceError: fetch is not defined in nodejs, consult our solution down here.
When did this error occur?
You’re trying to use the search for javascript. But when you’re absolutely unable to successfully decline the promise is not solved:
ReferenceError: fetch is not defined
In this case, it is a matter of configuration. You are facing with the ”ReferenceError: fetch is not defined in nodejs” issue because the fetch()
method is being used in an environment but it is not accepted, most frequently NodeJs. Let’s stay tuned for our article below.
How To Solve “ReferenceError: fetch is not defined in nodejs”
To solve this problem, you need to use node-fetch same as an external module. Native support for the fetch API is just available in browsers because it is not implemented in Node.js. Then you just need add the line below to the top of the files where you are calling the fetch API after installing the module.
Solution 1: Install node-fetch
The easy way to fix the ”ReferenceError: fetch is not defined in nodejs” is using node-fetch in your nodeJs application. You need to install it in your node application then copy the line below and put at the top of the files where fetch API is used:
npm install node-fetch
const fetch = require("node-fetch");
Solution 2: Accessible with a global scope
To processes quickly so you can get access to the global perimeter.
global.fetch = require("node-fetch");
Solution 3: Use cross-fetch
Besides, you can even use cross-fetch to solve this problem.
npm install --save cross-fetch
Now, you can import and use the module the same way you would use the fetch() method in your browser.
import fetch from 'cross-fetch';
fetch('//api.github.com/users/lquixada')
.then(res => {
if (res.status >= 400) {
throw new Error("Bad response from server");
}
return res.json();
})
.then(user => {
console.log(user);
})
.catch(err => {
console.error(err);
});
or:
import fetch from 'cross-fetch';
(async () => {
try {
const res = await fetch('//api.github.com/users/lquixada');
if (res.status >= 400) {
throw new Error("Bad response from server");
}
const user = await res.json();
console.log(user);
} catch (err) {
console.error(err);
}
})();
Solution 4: Use the built-in https module of Node
If you don’t want to use the above solutions, you can use the built-in https module of Node. Look at the following example:
const https = require('https');
const req = https.get("https://api.ipify.org?format=json", res => {
let data = "";
res.on('data', d => {
data += d
});
res.on('end', () => {
obj = JSON.parse(data);
console.log(obj.ip);
});
});
This solution is very simple, but it is effective for you. It will help you resolve your problem and make your program run well. Let’s try it to get your desired results.
Conclusion
Up here are the ways to solve the problem ReferenceError: fetch is not defined in nodejs. We hope you find your fault soon and fix them. If you can’t find the error of the articles, please contact our group so that we can help you solve the problem. Thank you very much!
Read more
Leave a comment