node-fetch@3 在 Nestjs 中不支持,因为它是 ESM

pu *_*njo 5 node-fetch nestjs

我无法使用包“node-fetch@3”。我只需导入它,我的控制台就会记录错误:

const node_fetch_1 = require("node-fetch");
                     ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/control-panel-nest2/node_modules/node-fetch/src/index.js from /home/control-panel-nest2/dist/bimwize/document.service.js not supported.
Instead change the require of index.js in /home/control-panel-nest2/dist/bimwize/document.service.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/control-panel-nest2/dist/bimwize/document.service.js:19:22)
    at Object.<anonymous> (/home/control-panel-nest2/dist/bimwize/bimwize.module.js:14:28)
    at Object.<anonymous> (/home/control-panel-nest2/dist/user/user.module.js:11:26)
    at Object.<anonymous> (/home/control-panel-nest2/dist/app.module.js:17:23)
    at Object.<anonymous> (/home/control-panel-nest2/dist/main.js:6:22)
Run Code Online (Sandbox Code Playgroud)

我知道发生了什么事。node-fetch@3 仅支持 esm,但不支持 commonjs,nestjs 会将我所有的导入编译为 require。

我不知道如何解决这个问题。我尝试使用node-fetch@2,但node-fetch@2缺少一些我想要的功能。

小智 -4

Nestjs提供HTTP模块。这是文档网址: https: //docs.nestjs.com/techniques/http-module