Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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/**/*"
  ]
}
like image 640
Vinz243 Avatar asked Apr 30 '17 16:04

Vinz243


Video Answer


1 Answers

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';
like image 139
Paarth Avatar answered Nov 09 '22 02:11

Paarth