TypeScript — TS2307: Cannot find module or its type declarations
Fix TS2307 by correcting module resolution settings and installing the missing type declarations.
Symptoms
- error TS2307: Cannot find module './utils' or its corresponding type declarations
- Imports work at runtime but the build fails
- Path aliases resolve in the bundler but not in tsc
- A third-party package has no bundled types
Possible causes
- The module path or file extension is wrong
- A path alias is configured in the bundler but not in tsconfig paths
- The dependency or its @types package is not installed
- moduleResolution mode does not match how the code imports
Diagnosis steps
Read the exact module specifier
npx tsc --noEmitInterpretation
The message shows the specifier tsc could not resolve, which distinguishes a wrong path from a missing package.
Compare tsconfig paths with the bundler
cat tsconfig.json | grep -A5 paths ; cat vite.config.tsInterpretation
Aliases must be declared for the type checker as well, or imports resolve at build time but not for tsc.
Install missing types
npm i -D @types/<pkg>Interpretation
A package without bundled declarations needs a types package, or a local declaration file.
Root cause
The type checker could not resolve the import to a file or declaration, because the path, alias, moduleResolution mode or installed types do not line up.
Fix
Correct the import path, declare matching paths in tsconfig, install the missing @types package or add a local declaration, and choose a moduleResolution mode consistent with your imports.
Verify the fix
Rerun the type checker and confirm the import resolves, then build and run to confirm the module loads at runtime too.
Symptom walkthrough
The compiler reports TS2307 for an import it cannot resolve. The same import may work at runtime because the bundler resolves it differently, which is why the failure can seem inconsistent. Typical causes are a missing path alias in tsconfig, an uninstalled dependency or its types, or a moduleResolution mode that assumes a different import style.
Fix in practice
Make resolution consistent across the type checker and the bundler: fix the specifier, add matching paths in tsconfig, install the types package or a local declaration, and set moduleResolution to the mode your imports require. Aligning the two tools removes the runtime-versus-compile discrepancy.
Verification
Rerun the type checker and confirm the import resolves, then build and run the application to confirm the module loads at runtime as well. Both phases passing proves resolution is aligned.
Diagnostic commands
Check the behavior with commands like these:
npx tsc --noEmit
cat tsconfig.json | grep -A5 paths ; cat vite.config.ts
npm i -D @types/<pkg>