question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

node-fetch unusable with esm package

See original GitHub issue

Given an index.js file with import fetch from "node-fetch" When I run cli command node -r esm index.js Then I should see the script import fetch and run.

Version 3 gives this error.
 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13) {
 code: 'ERR_REQUIRE_ESM'
}

Now, I’ve read the similar threads on this. One thing that hasn’t been mentioned, that the maintainers overlook, is there are still MAJOR projects out there that can’t be used with ESM directly. Cucumber.js is one of them. The top level project must be CJS for cucumber to work, but you can convince it to work seemlessly with the ESM package.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:12

github_iconTop GitHub Comments

10reactions
tenbitscommented, Sep 26, 2021

This is extremely opinioned position, which shouldn’t be the case for the lib, as both should be supported, while they are and will be both first-class module systems. A consumer should be able to use the library disregarding of which module system he/she uses. This is even disrespectful to those, who for any of the reasons use cjs.

2reactions
jimmywartingcommented, Nov 2, 2021

we also have similar cjs/esm issues open that are basically a dupl of this, so closing this one… There isn’t any issue with node-fetch itself This (now) ESM-only package works as intended. if you have a problem with using ESM-only packages or any other solution highlighted in #1227 then it’s really up to you to fix it

There is also a discussion about having a exposed cjs file that only export fetch (not headers, Response and Request) that is related to this: #1327

Read more comments on GitHub >

github_iconTop Results From Across the Web

ESM only breaks everything · Issue #1263 · node-fetch/node ...
ESM is not ready to have a library as major as node-fetch go ESM only. The workarounds you list in the README will...
Read more >
node-fetch@3 Not support in nestjs because it is ESM
I can't use the package "node-fetch@3". I just import it and my console will log the error: const node_fetch_1 = require("node-fetch"); ...
Read more >
The Fetch API is finally coming to Node.js - LogRocket Blog
It's been a long time coming, but the Fetch API is now available in Node.js core. Learn why it took so long and...
Read more >
Node Modules at War: Why CommonJS and ES ... - Code Red
Top-level await occurs during the execution phase of the module graph. At this point all resources have already been fetched and linked. There ......
Read more >
esm-fetch - npm Package Health Analysis - Snyk
The npm package esm-fetch was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found