转译节点获取需要 returns 错误

Transpiling node-fetch require returns error

我 运行 遇到了 node-fetch 库的问题。 新版本(3.0.3)的package.json中有"type": "module"。这是一个问题,因为我将我的代码从 ES6 转换为使用 require 的常规 js,然后我得到 ...firebase_datasource.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. 我可以使用 node-fetch 的早期版本,它的 package.json 中不包含 "type": "module",但那不是解决方案。 正确的处理方式是什么?

node-fetch's README 说明要做什么:

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));