我只是使用
const fetch = require('node-fetch')我得到了
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\Alex\Desktop\rollbot\node_modules\node-fetch\src\index.js from C:\Users\Alex\Desktop\rollbot\index.js not supported.
Instead change the require of C:\Users\Alex\Desktop\rollbot\node_modules\node-fetch\src\index.js in C:\Users\Alex\Desktop\rollbot\index.js to a
dynamic import() which is available in all CommonJS modules.
{
code: 'ERR_REQUIRE_ESM'
}我所有的其他包都可以工作,只有node-fetch可以做到这一点。要使用node-fetch,我应该怎么做?
发布于 2021-09-07 11:24:42
从node-fetch package readme
节点获取是一个仅限ESM的模块-您不能使用require导入它。我们建议您继续使用由CommonJS构建的v2,除非您自己使用ESM。我们将继续发布它的关键错误修复。
如果你想降级到require,那就降级到v2。
另一种选择是使用异步import('node-fetch').then(...)
发布于 2021-10-01 19:25:44
在我的例子中,我个人发现请求(“交叉获取”)更容易。文档在这里:cross-fetch
它可以在CommonJS和ES6模块中使用。
发布于 2022-01-30 18:01:30
我发现最新版本的node-fetch默认更改为ES模块。您可以执行以下操作:
使用npm uninstall node-fetch
npm install node-fetch@2它对我起作用了!
https://stackoverflow.com/questions/69087292
复制相似问题