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
Check whether the package ships its own types via a 'types' field in its package.json
Install community types if missing: npm install --save-dev @types/package-name
If no @types package exists, write a custom declaration: declare module 'package-name'; in a *.d.ts file
Ensure tsconfig.json's include array covers the folder containing your custom .d.ts files
Verify moduleResolution in tsconfig.json is set to 'node' or 'bundler' as appropriate
Restart the TypeScript server in the editor after adding new declarations
For monorepos, double-check paths and baseUrl correctly map the module
Found an issue with this solution?