Fix - ReferenceError fetch is not defined in NodeJs

avatar

Borislav Hadzhiev

Tue Oct 19 20212 min read

Fix - ReferenceError fetch is not defined in NodeJs #

The "ReferenceError: fetch is not defined" error occurs when the fetch() method is used in an environment where it's not supported - most commonly NodeJs. To solve the error install and import the node-fetch package, which provides a fetch() compatible API on NodeJs runtime.

referenceerror-fetch-is-not-defined

To solve the "ReferenceError: fetch is not defined" error install and import the node-fetch package.

If your project does not have a package.json file, create one first in your project's root directory:

shell
# 👇️ only run this if you don't have package.json file yet npm init -y

Now install the node-fetch library.

shell
npm install node-fetch

Now you can import and use the module just like you would use the fetch() method in your browser.

index.js
import fetch from 'node-fetch'; 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); } } console.log(await getUser());

Note that, at the time of writing, to use ES6 module imports and exports in a NodeJs project, you have to set the type property to module in your package.json file:

package.json
{ "type": "module", // ... 👇️ rest }
If you're using TypeScript, you don't have to install types for the node-fetchpackage as they are included by default.

If I run my NodeJs script now, I get the result from calling the API back.

fetch success

The more recent versions of the node-fetch package are only compatible with the ES6 Modules syntax of import/export. If you are using an older NodeJs version, simply install version 2 of the node-fetch package.

Fix - ReferenceError fetch is not defined in NodeJs (older versions) #

Only do this if you are using an older NodeJs version and want to use the require syntax instead of import / export.

bash
npm install node-fetch@2

We install version 2 of the node-fetch package.

Make sure you don't have the type property set to module in your package.json file.

Now you can import the fetch package using the older require function.

index.js
// 👇️ Using older require syntax const fetch = require('node-fetch'); 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); } }

We had to install version 2 of the node-fetch package to be able to use the require syntax in our NodeJs application.

It's best to stay consistent with imports between your client and server side code, but if you have to support an older version of NodeJs, this approach gets the job done.
Use the search field on my Home Page to filter through my more than 1,000 articles.