akabekobeko / npm-icon-gen

Icon file generator for Windows, macOS, Web
MIT License
157 stars 29 forks source link

Invalid types on esm style typescript nodejs loading #166

Open tom2strobl opened 2 months ago

tom2strobl commented 2 months ago

Hi there,

thanks for the library! I recently upgraded to node 20.x and switched to a type: "module" based package.json with typescript. Now when trying to

import iconGen from 'icon-gen'
await iconGen(resourcesSourcePath, resourcesDestinationPath, options)

the code runs, but typescript in VSCode says:

iconGen
--------
This expression is not callable.
  Type 'typeof import("<somepath>/node_modules/icon-gen/dist/lib/index")' has no call signatures.ts(2349)

A quick check shows this: https://arethetypeswrong.github.io/?p=icon-gen%405.0.0

As an intermediate workaround one can do this:

import { createRequire } from 'module'

const require = createRequire(import.meta.url)
const iconGen = require('icon-gen')

but that's not really preferable in the long run.

So it seems like the types are off due to the dual export somehow?

tom2strobl commented 2 months ago

fuse.js does it like this, which is quite readable and works nicely:

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