Error: Require() of Es Modules Is Not Supported When Importing Node-Fetch

Error: require() of ES modules is not supported when importing node-fetch

node-fetch v3 is ESM-only: https://github.com/node-fetch/node-fetch#loading-and-configuring-the-module. The esm module you’re adding is for adding ESM compatibility, but it’s unnecessary now that Node 12+ supports ESM natively; and it doesn’t work with ESM-only packages like node-fetch 3+.

To fix your issue:

  1. Remove the esm package.
  2. Add "type": "module" to your package.json.

And that’s it. Then when you run node server.js it should work.

Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported

The current version of node-fetch is ONLY compatible with an ESM import (using import), not from CommonJS modules using require().

You have these choices to fix:

  1. Switch your project to an ESM module and load it with import fetch from 'node-fetch';.

  2. In a very recent version of nodejs, you can dynamically import an ESM module into a CommonJS module using let fetch = await import('node-fetch').

  3. Use the v2 version of node-fetch that still supports being loaded with require() as explained here in the doc.

require('node-fetch') gives ERR_REQUIRE_ESM

From the node-fetch package readme:

node-fetch is an ESM-only module - you are not able to import it with
require. We recommend you stay on v2 which is built with CommonJS
unless you use ESM yourself. We will continue to publish critical bug
fixes for it.

If you want to require it, then downgrade to v2.

The other option you have is to use async import('node-fetch').then(...)

Error [ERR_REQUIRE_ESM]: require() of ES Module not supported

node-fetch v3 recently stopped support for the require way of importing it in favor of ES Modules. You'll need to use ESM imports now, like:

import fetch from "node-fetch";

at the top of your file.

require not supported even though I'm using import

How I fixed this problem:

Uninstall node-fetch and the types,

npm uninstall node-fetch
npm uninstall @types/node-fetch

Install any 2. version of node-fetch, I used node-fetch@^2.6.1 with @types/node-fetch@2.5.12

Installation:

npm install node-fetch@^2.6.1
npm install --save-dev @types/node-fetch@2.5.12

How can I resolve ERR_REQUIRE_ESM error when using this simple node-fetch query?

The error really says it all. Since node-fetch is an ES module, you shouldn't use the require syntax to import it, but the import syntax:

import fetch from 'node-fetch';

Note:

Older versions of node-fetch are still CommonJS packages (i.e., can be used with require), so if you downgrade your dependency to some 2.x version, your code should work as-is.

Transpiling node-fetch require returns error

node-fetch's README explains what to do:

CommonJS

node-fetch from v3 is an ESM-only module - you are not able to import it with require().

If you cannot switch to ESM, please use v2 which remains compatible with CommonJS. Critical bug fixes will continue to be published for v2.

npm install node-fetch@2

Alternatively, you can use the async import() function from CommonJS to load node-fetch asynchronously:

// mod.cjs
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));


Related Topics



Leave a reply



Submit