Cannot invoke an expression whose type lacks a call signature when using node-fetch

fetch-api, node.js, typescript

Solution

You've imported the entire module rather than the default export, the fetch function. You're trying to call the entire module as a function which doesn't work.

Instead of

import * as fetch from 'node-fetch';

try

import fetch from 'node-fetch';

Problem

I'm trying to get `node-fetch` to work in my typescript project: ``` import * as fetch from 'node-fetch'; import * as assert from 'assert'; export class DatabaseConfigurator { private url: string; getNode (): Promise<string> { return fetch(`${this.url}/_membership`).then((response: fetch.Response) => { return response.json(); }).then((res: any) => { assert.equal(res.all_nodes.length, 1); return res.all_nodes[0]; }); } } ``` And I get: ``` Cannot invoke an expression whose type lacks a call signature. Type 'typeof "/home/vinz243/compactd/node_modules/@types/node-fetch/index"' has no compatible call signatures. ``` When the definition i installed seems ok (`node_modules/@types/node-fetch/index.d.ts`): ``` ... export default function fetch(url: string | Request, init?: RequestInit): Promise<Response>; ``` My `tsconfig.json` ``` { "compilerOptions": { "sourceMap": true, "outDir": "./dist/", "noImplicitAny": true, "module": "commonjs", "target": "es6", "allowJs": true }, "include": [ "./src/**/*" ] } ```

Original source