Home > Net >  TypeError: .... is not a constructor in NodeJs & Typescript
TypeError: .... is not a constructor in NodeJs & Typescript

Time:01-24

i feel like i have read the whole internet on this, but i just cannot figure out what the problem is: i have a nodejs / typescript Project here, and need to use a Library called KDTree. from its readme i have to use it like so:

const { default: KDTree } = require("kd-tree-ts");
const tree = new KDTree;

since my through my tsconfig i need to add libraries via ìmport ... from '...' i do it like so:

import KDTree from 'kd-tree-ts'
    const tree = new KDTree

also tried:

import * as  KDTree from 'kd-tree-ts'
    const tree = new KDTree.default

but: on the new KDTree line i get this error: TypeError: KDTree is not a constructor (but it is!--> https://github.com/NickNaumenko/kd-tree-ts/blob/develop/src/kdTree.ts ) and i have just 0 idea where and what it causes. i do feel like its something in the tsconfig but for this project i cannot change it.

this is the tsconfig:

{
  "compilerOptions": {
    "target": "es2016",
    "lib": [
      "es2019"
    ], 
    "module": "ESNext", 
    "rootDir": "src",
    "moduleResolution": "node",
    "outDir": "./bin",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitAny": true,
    "skipLibCheck": true
  }
}

it anyone could point me in the right rirection it would be awesome!

CodePudding user response:

Tried a minimum project and this seems to work:

import KDTree from 'kd-tree-ts';
const tree = new KDTree()

Notice that you need to use the parenthesis when doing new KDTree()

CodePudding user response:

{
  "compilerOptions": {
    "target": "es2016",
    "lib": [
      "es2019"
    ], 
    "module": "ESNext", 
    "rootDir": "src",
    "moduleResolution": "node",
    "outDir": "./bin",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitAny": true,
    "skipLibCheck": true
  }
}

Add "allowSyntheticDefaultImports": true, after "esModuleInterop" to support synthetic default imports.

  • Related