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.

[3.0.0-beta.10] Can't import node-fetch when using TypeScript in CJS mode

See original GitHub issue

When upgrading node-fetch to v3.0.0-beta.10 in a TypeScript project which compile modules in CommonJS mode, the following error occured:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /tmp/node-fetch-test/node_modules/node-fetch/src/index.js
require() of ES modules is not supported.
require() of /tmp/node-fetch-test/node_modules/node-fetch/src/index.js from /tmp/node-fetch-test/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename /tmp/node-fetch-test/node_modules/node-fetch/src/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /node-fetch-test/node_modules/node-fetch/package.json.

Reproduction

Steps to reproduce the behavior:

  1. Create an empty project:
npm init
npm i -D typescript
npm i node-fetch@3.0.0-beta.10
npx tsc --init
  1. Create a file index.ts with the following code:
import fetch from "node-fetch";

(async () => {
    const res = await fetch("https://api.github.com/repos/touchifyapp/player/releases/latest", {
        headers: { Accept: "application/vnd.github.v3+json" }
    });

    console.log(await res.json());
})();
  1. Run the project:
tsc -p .
node index.js

Expected behavior

It should be able to import node-fetch correctly.

Many projects are using commonjs. When using TypeScript in cjs mode, it is not possible to mix require and import statements. Even in traditional javascript projects, importing an ES module is a mess. you have to use the async import() syntax, you can’t import the module as usual.

I think we should provide both ESM and CJS versions of the package to ensure all users can migrate to node-fetch v3. We can use package.json exports configuration to ensure projects are using the appropriate version of the module depending on their configuration.

The simplest and quickest method could be to use esbuild which can transpile esm modules to cjs.

Your Environment

software version
node-fetch 3.0.0-beta.10
node 14.15.3
npm 7.19.1
Operating System WSL 2
typescript 4.3.5

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
hjdhjdcommented, Aug 3, 2021

Mostly I needed #715. 😄 But using a few of the other features / evolutions built into 3.0.

To be honest - there’s nothing you wrote that I wouldn’t feel the same way about in your position. I think you’re right that it’s beta and a major release and therefore expecting breaking changes is not unreasonable. But it does make me work more right now while I try to support my own codebase. 😄 I knew what I was getting into when I shifted to 3.0…it’s on me to adapt, but also try to nudge where I can to get a bit more breathing room!

The platform in question I’m supporting is Homebridge. There’s an open PR to support ESM, but it’ll be a while before it gets pulled into the mainline…and that’s what’s creating more hand-wringing for me.

I’ll try the shim you suggested out and see if I can make it work.

0reactions
jimmywartingcommented, Aug 13, 2021

Locking b/c it’s nicer to keep the discussion in one place. (We still keep a open mind and looking for a solution - it dose not mean we will overlook this Issue)

Feel free to keep the discussion in #1227 where most of the discussion is already happening

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't import node-fetch into Typescript
I'm trying to import node-fetch into my Typescript project but I fall into this error: [ERR_REQUIRE_ESM]: Must use import to load ES Module...
Read more >
node-fetch - npm
node -fetch is an ESM-only module - you are not able to import it with require . We recommend you stay on v2...
Read more >
node-fetch require() of es module - You.com | The search engine ...
I'm creating a program to analyze security camera streams and got stuck on the very first line. At the moment my .js file...
Read more >
Firebase JavaScript SDK Release Notes - Google
Fixed incorrect import of node-fetch polyfill that caused uncaught exceptions when using Cloud Storage and the firestore/lite package from Cloud Firestore.
Read more >
Gatsby Changelog | 5.3.0
Please note: The TypeScript variants of gatsby-config and gatsby-node do not support ESM yet. We plan on adding support in a future minor...
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