krisk / Fuse

Lightweight fuzzy-search, in JavaScript
https://fusejs.io/
Apache License 2.0
18.26k stars 768 forks source link

TypeScript import issue #780

Open joergbaier opened 2 weeks ago

joergbaier commented 2 weeks ago

Description of the bug

fuse.js v7.0.0

import

import Fuse from "fuse.js";

tsconfig.json (https://github.com/tsconfig/bases/blob/main/bases/node20.json)

"module": "node16",
"moduleResolution": "node16",

package.json

"type": "commonjs",
"engines": {
  "node": "20.x"
},

typescript error

error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("fuse.js")' call instead. To convert this file to an ECMAScript module, change its file extension to '.mts' or create a local package.json file with { "type": "module" }.


Based on comments in this thread, this package incorrectly exports typings. https://github.com/microsoft/TypeScript/issues/53045#issuecomment-1452605559

The error was resolved when I copied fuse.d.ts to fuse.d.cts and manually edited node_modules/fuse.js/package.json

"exports": {
  ".": {
    "import": {
      "default":"./dist/fuse.mjs",
      "types": "./dist/fuse.d.ts"
    },
    "require": {
      "default":"./dist/fuse.cjs",
      "types": "./dist/fuse.d.cts"
    }
  }
}

Seems like fuse.js needs to expose d.cts and d.ts.

sebastianluczak commented 2 hours ago

I'm also affected by this issue. Is there any plan to resolve it in near future?