Search code examples
javascripttypescript

How do I import a Javascript library into Typescript when the package has a declaration file


I am trying to use the type definitions in the callbag library. This library declares its type definition file in its package.json. It is not uploaded to DefinitelyTyped. When I try to import the library, I get the error:

Cannot find module 'callbag'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?

This is how I import it:

import type { Sink } from 'callbag';

Here is my tsconfig.json:

{
  "compilerOptions": {
    "module": "es6",
    "noEmitOnError": true,
    "outDir": "./dist",
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "target": "es5"
  },
  "include": ["./src/**/*"]
}

I also tried adding "types": ["callbag"] to the tsconfig.json, but it didn't work.

How do I get typescript to recognize the types for this library?


Solution

  • As posted in the comments, I needed to add "moduleResolution": "node" to my tsconfig.json. Not sure why because nothing that I'm working on is related to Node.js. But that's what worked.