ReferenceError: fetch is not defined error occurs when trying to “use the fetch() function in a Node.js environment, but fetch is not natively available in Node.js.”
In browsers, the fetch API provides a simple way to make network requests. But in Node.js, it is not available.
Reproduce the error
const fetch = require('node-fetch');
fetch('https://askjavascript.com')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
Output
How to fix the error
To fix the fetch is not defined error in Node.js, use libraries like “axios”, “node-fetch”, or the “built-in http and https modules to make HTTP requests.”
To use the fetch function in Node.js, you can install the node-fetch package:
npm install node-fetch
Now, we can use the fetch() method in Node.js.
If you are using ES6 imports:
import fetch from 'node-fetch';
This will give you a fetch() function in Node.js that works similarly to the browser’s fetch API.
Related posts
error: cannot find module ‘express’
cannot find module ‘fs/promises’ in Node.js
error: cannot find module ‘node:events’ in Node.js
nodemon clean exit – waiting for changes before restart
yarn requires node.js 4.0 or higher to be installed

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.