When you ran your code, you saw the error messages: “ReferenceError: fetch is not defined“
As a result, after some research, we will present potential alternatives to programmers. Do not think too much, and let’s get going on correcting this error.
Under What Conditions Would This Issue Occur?
In general, fetch was created for one browser and converted to node.js in a third-party module that you appear to be lacking.
You may experience this issue while using fetch in the NodeJS application.
ReferenceError: fetch is not defined
Fix The Problem: ReferenceError: Fetch Is Not Defined
You’ll need to utilize some external module, such as node-fetch, for this. Install this in the Node application, and add some lines to the head of your files that use the retrieve API.
Let’s take a closer look at stages using the three techniques listed below.
Method 1
The first way is to use the cross-fetch.
npm install --save cross-fetch
With promises, you can use this script.
import fetch from 'cross-fetch';
// Or just: import 'cross-fetch/polyfill';
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);
});
In the case of async/await, type this:
import fetch from 'cross-fetch';
// Or just: import 'cross-fetch/polyfill';
(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);
}
})();
Method 2
You can use two syntaxes to address this problem.
Install this in the Node application as follows. Then add the second line
npm install node-fetch
const fetch = require("node-fetch");
Alternatively, you may add the following line at the files in which you are utilizing the retrieve API:
import fetch from "node-fetch";
Now you may implement fetch in the NodeJS application.
Method 3
Suppose it must be available on a worldwide scale. This is a fast and dirty workaround; please avoid using this in the codebase.
global.fetch = require("node-fetch");
Conclusion
In general, resolving the error “ReferenceError: fetch is not defined” is indeed not difficult.
We are confident that our answer will help you complete your project promptly. Hopefully, you’ll be able to produce even more incredible intellectual goods with nodejs.
Related articles
- Top Ways To Create A User-Friendly Online Property Search For Your Real Estate Clients
If you’re running a real estate business, you’re well aware that pretty much most of the paperwork has become automated and it’s time for you really get an education on what you need to use in terms of technology. This is important to make your online presence and services stand out from the competition. Like […]
- List Education Websites for Students, providing a variety of materials and completely free
Everyone would like to get the highest quality of education in order to fulfill their goals. But the more an institution is of high quality and reputable, the more fees they charge. Students typically leave their education in a state of nil and work blue collar jobs to achieve their primary needs. Additionally, they collect […]
- Simple solution to correct the requests.exceptions.ConnectionError: (‘Connection aborted.’ RemoteDisconnected(‘Remote end closed connection without response’)) issue
Python is a popular programming language that can be used widely in a lot of applications. Python is also a good choice as a programming language depending on user background and perspective. Because it is used widely and popular, if you find any errors when using Python. It is a common problem, you face the […]
- “[Errno 61] Connection refused” is occurring even, the program is connecting with the port well and the socket is running in the interfaces.
If you see the “[Errno 61] Connection refused” issue although you checked the program, port, socket and interfaces. Although your program of Python works well in the server and the client, they are installed at the same device. The local IP from my device is connecting with the clients but this IP is not connected […]
- Description “Return by Reference”.
C++ is considered not only as a language of Object Oriented Programming, but also an intermediate level language. It identifies both high and low level languages. It became easy and widely used in computer programs and that is the reason why we should understand the definition and its function as well. Such as Return by […]