使用 require 时出现打字稿错误 - 该表达式不可调用。类型“typeof import(...)”没有调用签名.ts(2349)

Oba*_*Api 4 node.js typescript node-fetch

我正在尝试运行这个脚本

const fetch = require('node-fetch');

function test() {
  fetch('https://google.com')
    .then(res => res.text())
    .then(text => console.log(text))
}

test();
Run Code Online (Sandbox Code Playgroud)

但我收到这个错误

该表达式不可调用。类型 'typeof import("(...)/node_modules/node-fetch/@types/index")' 没有调用签名.ts(2349)

虽然当我使用 import 时它可以工作

import fetch from 'node-fetch';
Run Code Online (Sandbox Code Playgroud)

为什么以及如何解决它?

Pur*_*ret 7

根据spender评论,您可以更改使用此解构的要求:

const {default : fetch} = require('node-fetch');
Run Code Online (Sandbox Code Playgroud)

这在类似的情况下对我有用(在节点中使用 axios,它具有类似的 API)