[3.0.0-beta.10] Can't import node-fetch when using TypeScript in CJS mode
See original GitHub issueWhen 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:
- Create an empty project:
npm init
npm i -D typescript
npm i node-fetch@3.0.0-beta.10
npx tsc --init
- 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());
})();
- 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:
- Created 2 years ago
- Reactions:4
- Comments:11 (5 by maintainers)
Top GitHub Comments
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.
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