11

Copied from here: https://github.com/node-fetch/node-fetch#json ... my code:

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

async function doFetch() {
    const response = await fetch('https://api.github.com/users/github');
    const data = await response.json();

    console.log(data);
}

doFetch()

Error message -- referring to the fetch('https://...) call:

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

I've searched this and other sites, and this error seems pretty common with many modules, although I can only find fixes for examples using import instead of require. I'm guessing it's pretty simple, but I just can't figure out how to fix this with require syntax.

What am I doing wrong? Thanks in advance!


If it matters: JavaScript (not TypeScript). node.js. VSCode on Mac. The error message is generated by the dbaeumer.vscode-eslint plugin.


DavidT
  • 655
  • 7
  • 19

1 Answers1

0

actual documentation says:

node-fetch from v3 is an ESM-only module - you are not able to import it with require()

(even the place where copied from uses import now)

so you should use the import way:

import fetch from 'node-fetch';

if you really insist on the require way, use this hack:

const {default: fetch} = require('node-fetch');
Michal Miky Jankovský
  • 3,089
  • 1
  • 35
  • 36