node-fetch -- typeof import("[...]/node-fetch/index")' has no call signatures
Asked Answered
F

1

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.


Franck answered 26/4, 2021 at 16:12 Comment(0)
V
1

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');
Vierno answered 30/1, 2023 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.