Skip to main content

TypeScript · 6 common causes

TS2307: Cannot find module

Updated

TypeScript could not resolve the import to a module, or found the module but no type declarations for it.

The exact message

src/index.ts:2:23 - error TS2307: Cannot find module './utils' or its corresponding type declarations.

2 import { format } from './utils';
                        ~~~~~~~~~

Found 1 error in src/index.ts:2

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is TS2307: Cannot find module.

How to fix it

Cause 1 of 6

The package ships no types and no @types package is installed.

Install the community types if they exist: npm i -D @types/<package>. If none exist, declare the module yourself in a d.ts file inside your project — declare module 'the-package'; — which silences the error at the cost of treating it as any, so add real signatures for the parts you use.

Cause 2 of 6

The path or the extension is wrong.

Relative specifiers resolve against the importing file, not the project root. Check the case too — enable forceConsistentCasingInFileNames so a mismatch that only breaks on Linux fails on your machine as well.

Cause 3 of 6

A path alias is configured for the compiler but not for the runtime.

paths in tsconfig.json teaches only tsc. The bundler or runner needs the same mapping: resolve.alias in Vite, moduleNameMapper in Jest, tsconfig-paths for ts-node. Configuring one and not the other produces this error in half your tools.

Cause 4 of 6

moduleResolution does not match how the package publishes its types.

Packages that use an exports map need "moduleResolution": "bundler" or "nodenext". The legacy "node" setting cannot see types behind an exports map at all, which is why a modern package appears untyped.

Cause 5 of 6

The editor is using a different TypeScript than the build.

In VS Code, run 'TypeScript: Select TypeScript Version' and choose 'Use Workspace Version', then 'TypeScript: Restart TS Server' after installing types. An error that only your editor shows is nearly always this.

Cause 6 of 6

You are importing a non-code asset.

import styles from './card.module.css' needs an ambient declaration. Frameworks ship one — reference vite/client or the framework's types in tsconfig, or write your own declare module '*.css'.

Stopping it coming back

Keep tsconfig path aliases and the bundler's aliases defined from one shared source, and run tsc --noEmit in CI so an editor-only resolution difference cannot reach main.

TS2307: Cannot find module — common questions

The code runs but tsc still complains. Why?
The bundler and the compiler resolve modules independently. The bundler found the file; TypeScript did not, usually because of a missing path alias or a moduleResolution mismatch. Both need to agree.
Is declaring a module as any acceptable?
As a stopgap, yes — it unblocks the build and is honest about what you know. Write it in a dedicated types file with a comment, and replace it with real signatures for the functions you actually call rather than leaving it forever.
Why does this only fail in CI?
Almost always case sensitivity, or a dependency in devDependencies that CI did not install. Check the exact casing of the file against the import, and confirm the install step covers the packages the type check needs.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor