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:
- Remove the
esm
package. - Add
"type": "module"
to yourpackage.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:
Switch your project to an ESM module and load it with
import fetch from 'node-fetch';
.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')
.Use the v2 version of
node-fetch
that still supports being loaded withrequire()
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 withrequire()
.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 loadnode-fetch
asynchronously:// mod.cjs
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
Related Topics
In R, How to Display Value on the Links/Paths of Sankey Graph
JavaScript Date.Utc() Function Is Off by a Month
Converting JSON Format to CSV to Upload Data Table in R to Produce D3 Bubble Chart
How to Prevent the Backspace Key from Navigating Back
Reading JavaScript Variable into Shiny/R on App Load
How to Use Source: Function()... and Ajax in Jquery UI Autocomplete
Invert Y Axis of L:Crs.Simple Map on Vue2-Leaflet
JavaScript Call to Swift from Uiwebview
How to Convert Special Utf-8 Chars to Their Iso-8859-1 Equivalent Using JavaScript
Time Conversion Between Ruby on Rails and JavaScript Vice Versa
What's the Difference Between Console.Dir and Console.Log
Draggable Line Chart in R/Shiny
Get Word Under Tap from Uiwebview Using JavaScript
Cannot Set Boolean Values in Localstorage
Resolving Require Paths with Webpack
How to Intercept Xmlhttprequests from a Greasemonkey Script