ReferenceError: fetch is not defined in NodeJs

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
4 min

banner

# Table of Contents

  1. ReferenceError: fetch is not defined in NodeJs
  2. ReferenceError fetch is not defined in NodeJs (older versions)
  3. Update your Node.js version, so you can use native fetch

# ReferenceError: fetch is not defined in NodeJs

The "ReferenceError: fetch is not defined" occurs when the fetch() method is used in an environment where it's not supported - most commonly Node.js.

To solve the error, install and import the node-fetch package, which provides a fetch() compatible API in the Node.js runtime.

referenceerror-fetch-is-not-defined

Note that the global fetch variable is available in Node.js starting at version 18.

You can check your version of Node.js with the node -v command.

shell
node -v

get node js version

If you use a Node.js version older than 18, you can either download and install the LTS version from nodejs.org or install and use the node-fetch package.

If you're stuck with a Node.js version older than 18, solve the error by using the node-fetch package.

If your project doesn't have a package.json file, create one 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 by running the following command.

shell
# ๐Ÿ‘‡๏ธ with NPM npm install node-fetch # ๐Ÿ‘‡๏ธ only if you use TypeScript npm install @types/node-fetch --save-dev # -------------------------------------------- # ๐Ÿ‘‡๏ธ with YARN yarn add node-fetch # ๐Ÿ‘‡๏ธ only if you use TypeScript yarn add @types/node-fetch --dev

Now you can import and use the module just like you would use the fetch() method in the 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());

using node fetch package

The code for this article is available on GitHub

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 I run my NodeJs script, I get the result from calling the API.

fetch success

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

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

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

bash
npm install node-fetch@2

We installed 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); } }
The code for this article is available on GitHub

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. However, this approach gets the job done if you have to support an older version of NodeJs.

# Update your Node.js version, so you can use native fetch

The fetch() method was added in Node.js starting with version 18.

You can use the node -v command to check your Node.js version.

shell
node -v

If you use an older version, I'd suggest you use the nvm package to manage your Node.js version in an intuitive way.

It is advised that you use the LTS Node.js version (20.11.1 at the time of writing).

If you already have nvm installed, install the LTS (long-term supported) version by issuing these commands.

shell
nvm install --lts nvm use --lts

If you don't have NVM installed, click on the link that relates to your operating system and follow the instructions.

Want to learn more about installing and using NVM? Check out these resources: Install NVM on macOS and Linux,Install NVM on Windows.

Once your Node.js version is 18 or more recent, you'll be able to use the native fetch() method without having to import it.

index.js
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());
The code for this article is available on GitHub

Alternatively, you can download the long-term supported version of Node.js from the official nodejs.org website.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev