Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript error when using require - This expression is not callable. Type 'typeof import(...)' has no call signatures.ts(2349)

I'm trying to run this script

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

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

test();

But i get this error

This expression is not callable. Type 'typeof import("(...)/node_modules/node-fetch/@types/index")' has no call signatures.ts(2349)

although it works when i use import

import fetch from 'node-fetch';

why and how to fix it?

like image 803
Oba Api Avatar asked Sep 13 '25 21:09

Oba Api


1 Answers

As per spender's comment, you can change the require to use this destructuring:

const {default : fetch} = require('node-fetch');

This has worked for me in a similar situation (using axios in node, which has a similar API)

like image 127
AncientSwordRage Avatar answered Sep 15 '25 12:09

AncientSwordRage