How to fix “ReferenceError: fetch is not defined” in Strapi

Last Updated:

If you’re getting a 500 error when calling Strapi APIs, you may see “ReferenceError: fetch is not defined” error from the Strapi server logs.

The logs look like the following:

0|strapi-project  | [2023-08-27 13:02:20.894] error: fetch is not defined
0|strapi-project  | ReferenceError: fetch is not defined
0|strapi-project  |     at Object.build (/var/strapi-project/src/plugins/revalidation/server/services/settings.js:87:21)
0|strapi-project  |     at processTicksAndRejections (node:internal/process/task_queues:96:5)
0|strapi-project  |     at async Object.build (/var/strapi-project/src/plugins/revalidation/server/controllers/settings.js:53:18)
0|strapi-project  |     at async returnBodyMiddleware (/var/strapi-project/node_modules/@strapi/strapi/lib/services/server/compose-endpoint.js:52:18)
0|strapi-project  |     at async policiesMiddleware (/var/strapi-project/node_modules/@strapi/strapi/lib/services/server/policy.js:24:5)
0|strapi-project  |     at async /var/strapi-project/node_modules/@strapi/strapi/lib/middlewares/body.js:58:9
0|strapi-project  |     at async /var/strapi-project/node_modules/@strapi/strapi/lib/middlewares/logger.js:9:5
0|strapi-project  |     at async /var/strapi-project/node_modules/@strapi/strapi/lib/middlewares/powered-by.js:16:5
0|strapi-project  |     at async cors (/var/strapi-project/node_modules/@koa/cors/index.js:107:16)
0|strapi-project  |     at async /var/strapi-project/node_modules/@strapi/strapi/lib/middlewares/errors.js:13:7

What’s causing this error

This error comes up because the system is trying to use the ‘fetch’ method—like when a Strapi plugin (the revalidation plugin, in my case) calls it—but can’t find the ‘fetch’ module.

This kind of problem is especially common if you’re using a version of NodeJS that’s older than 18, where the ‘fetch’ module isn’t readily available.

If you’re on Node version 18 and still see this error, you can try the suggestion mentioned at the bottom of the next section.

How To Fix “fetch is not defined” Error in Strapi

The issue is likely related to the version of Node.js you are currently using. If your Strapi project is version v4.3.9 or later, Strapi recommends using Node.js 18, as per the latest best practices and documentation available at the time of this writing.

To check what version of NodeJS you’re using, run:

node -v

If you’re using older versions of NodeJS but your Strapi project is compatible with NodeJS 18 or above (Node 18 is recommended for Strapi v4.3.9 and above), you can upgrade the NodeJS version.

My preferred approach is to install versions of NodeJS via Node Version Manager or NVM.

To install NVM:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash

The latest version and the command to install it can always be retrieved from the nvm git repository.

Once nvm is installed in your system, now you can install NodeJS 18 or above. To install NodeJS version 18, run the following:

nvm install 18

This will install NodeJS version 18 and make it available in the system.

Now you can switch to NodeJS version 18 using the following:

nvm use 18

After that, you need to rebuild your Strapi project.

If you’re already on Node version 18 and still get this error, you might try:

  • Delete all the files and directories inside your Strapi project where your build happens, including the hidden files
  • Pull the code from your Git repo again
  • Rebuild the app

This will make sure no obsolete node modules are present in your Strapi project.

Installing the node-fetch module

If you’re using the older versions of Strapi and Node, you need to install the node-fetch module.

Using NPM:

npm install node-fetch

If you use Typescript:

npm install @types/node-fetch --save-dev

Using Yarn:

yarn add node-fetch

If you’re using Typescript:

yarn add @types/node-fetch --dev

The fetch API method should be correctly imported before it’s used in the code. In a typical Strapi setup, this importation is usually managed by Strapi itself—meaning, if you’re working within the standard, out-of-the-box Strapi configurations and codebase, Strapi should take care of this for you.

import fetch from 'node-fetch';

However, if you’re diving into custom coding, adding plugins, or tweaking Strapi’s native code, you might need to ensure that fetch is properly imported wherever you decide to use it.

After making sure fetch is available, remember to rebuild your Strapi project to apply the changes.

Conclusion

The “ReferenceError: fetch is not defined” error is pretty common in NodeJS. It usually means that the system can’t find the node-fetch module. In newer versions of Node, like Node 18 and above, you usually don’t have to add this module yourself. But, when you’re working with Strapi, this error might pop up if you’re using an older version of NodeJS or if some of your node modules are outdated. So, keeping Strapi and NodeJS up to date and making sure to install node-fetch if needed, can help you avoid this error.

RECENT POSTS