Importing a JavaScript library in a TypeScript project throws 'Cannot find module' even though the package is installed and works fine at runtime.

Install or write missing type declarations for the package, and confirm tsconfig's module resolution settings match your project structure.

Step-by-Step Guide

1

Check whether the package ships its own types via a 'types' field in its package.json

2

Install community types if missing: npm install --save-dev @types/package-name

3

If no @types package exists, write a custom declaration: declare module 'package-name'; in a *.d.ts file

4

Ensure tsconfig.json's include array covers the folder containing your custom .d.ts files

5

Verify moduleResolution in tsconfig.json is set to 'node' or 'bundler' as appropriate

6

Restart the TypeScript server in the editor after adding new declarations

7

For monorepos, double-check paths and baseUrl correctly map the module

Found an issue with this solution?

Related Topics

custom type declaration filestsconfig module resolution modesmonorepo typescript paths config