Troubleshooting ReferenceError: fetch is not defined

ReferenceError: fetch is not defined means the JavaScript runtime does not provide a fetch function in the process that runs your code. In Node.js, check the runtime first. Node.js exposes fetch as a global in supported modern releases, while an older runtime or a test environment may need an explicit HTTP client.

Check whether your runtime has fetch

Start with a small capability check instead of installing a package. It reports the Node.js version, shows whether fetch exists, and makes a request to a self-contained data URL so the result does not depend on a third-party endpoint.

console.log({ node: process.version, fetch: typeof fetch });

const response = await fetch('data:application/json,%7B%22ok%22%3Atrue%7D');
console.log(await response.json());

Save the file as check-fetch.mjs and run node check-fetch.mjs.

The await expression pauses only this module until the response body is parsed. A successful function check does not prove that every network request will succeed, so check response.ok before you use response data from an HTTP service.

Use built-in fetch on a supported Node.js release

If the check prints function, use fetch directly.

const response = await fetch('https://api.github.com/repos/nodejs/node');

if (!response.ok) {
  throw new Error(`Request failed with ${response.status}`);
}

const repository = await response.json();
console.log(repository.full_name);

This code separates transport success from body parsing. The response object is available even for an HTTP error status, so response.ok gives you a clear failure boundary before the parsed data reaches the rest of your program.

Fix an older Node.js runtime

If typeof fetch prints undefined, the process is using a runtime without the global API. Upgrade the runtime when you control the deployment, then rerun the capability check in the same shell, container, continuous integration image, or test runner that raised the error.

When an upgrade is not available, install an HTTP client for that project and import it in the module that makes requests. Node-fetch is one option for code that needs a Fetch-compatible interface.

npm install node-fetch

Use the package import rather than assigning a hidden global. That keeps the dependency visible to the module and avoids making a production fix appear to work only because another file changed process state.

import fetch from 'node-fetch';

const response = await fetch('https://api.github.com/repos/nodejs/node');
console.log(response.status);

Check the environment that actually fails

A local terminal can have a newer Node.js release than the process that runs a build, test, serverless function, or container. Put the version and typeof fetch check beside the failing command to identify the environment that needs an upgrade or dependency.

  • Run node –version in the failing environment.
  • Run a script that logs typeof fetch.
  • Use global fetch when it is available, or import an HTTP client when it is not.

For a fuller introduction to HTTP requests, see our guide to getting data from a REST API in Node.js. It belongs after the diagnosis because choosing an HTTP method becomes useful once your runtime can make the request.

Why the error occurs

ReferenceError is JavaScript’s signal that code tried to read a name the active scope does not define.

Do not solve that by copying browser APIs into every project. Match the fix to the process you deploy, then keep the version check close to your build and deployment configuration so a future runtime change is easy to spot.

Frequently asked questions

Why is fetch not defined in Node.js?

The Node.js process does not expose fetch as a global. Check the Node.js version and the exact runtime that runs your code. Upgrade when you control it, or import an HTTP client when you cannot.

Should I install node-fetch on a modern Node.js release?

Usually no. If the runtime reports fetch as a function, use the built-in API. Add node-fetch only when your supported runtime or deployment environment lacks the global API.

Anurag Pandey
Anurag Pandey

Anurag Pandey writes Node.js and TypeScript guides focused on setup, files, JSON, npm, and debugging. He also covers React and TypeScript concepts.

Articles: 18