Sub topic 11
When the compiler cannot find a module
Assume you know basic JavaScript modules, TypeScript imports, and how to run tsc. A module is a file or package that another file can import. TypeScript reports TS2307 when it cannot locate the module or its type declarations.
What TS2307 means
Consider this project:
project/
src/
index.ts
format.tsIf index.ts contains this import, the path has a spelling error:
import { formatName } from "./formatt";
console.log(formatName("Ada"));Run the compiler:
$ npx tsc --noEmit --pretty false
src/index.ts(1,28): error TS2307: Cannot find module './formatt' or its corresponding type declarations.The compiler starts at src/index.ts, resolves ./formatt relative to that file, and looks for matching files such as formatt.ts, formatt.tsx, or formatt.d.ts. None exists. The fix is to correct the import:
import { formatName } from "./format";
console.log(formatName("Ada"));The ./ prefix means “from this file's directory.” A path without ./ or ../, such as "date-fns", normally refers to an installed package.
Follow the resolution path
When you see TS2307, check the import in this order:
- Check spelling and capitalization. A path that works on a case-insensitive filesystem can fail on Linux.
- Check the relative path. Count directories from the importing file. Use
../to move to the parent directory. - Check that the target file exists and exports the requested value.
- For a package import, check that the package is installed and listed in
dependenciesordevDependencies. - Check whether the package provides TypeScript declarations. Declarations describe JavaScript types in
.d.tsfiles. - Check
tsconfig.jsonsettings such asmodule,moduleResolution,baseUrl, andpaths.
flowchart TD
A[Import in source file] --> B{Relative path?}
B -->|Yes| C[Resolve from importing file]
B -->|No| D[Search installed package]
C --> E{Matching file exists?}
D --> F{Package and declarations available?}
E -->|No| G[Correct path or filename]
E -->|Yes| H[Check exports and types]
F -->|No| I[Install package or add declarations]
F -->|Yes| HPackages and declarations
Install a package with npm rather than writing a fake path:
npm install date-fnsThen import it by its package name:
import { format } from "date-fns";
const label = format(new Date(2026, 10, 25), "yyyy-MM-dd");
console.log(label);If you import a JavaScript package with no declarations, TypeScript may report TS7016 instead. You can install a community package such as @types/package-name, if one exists. As a last resort, add a carefully scoped declaration:
declare module "legacy-format" {
export function format(value: string): string;
}This tells the compiler about the API. It does not verify that the runtime package really exports that function.
Triage the configuration
Do not immediately add a broad declare module "*" declaration. It hides misspellings and removes useful type checking. Instead, inspect the effective configuration:
npx tsc --showConfigIf the project uses path aliases, configure them consistently with the runtime or bundler. A paths entry helps TypeScript resolve an alias, but it does not automatically teach Node.js how to load that alias.
Exercise
An import ../utils/math produces TS2307, but src/utils/math.ts exists. What should you check first?
Show answer
Check the importing file's directory, because the relative path may point to a different location than src/utils/math.ts; then check filename capitalization and the compiler's included files.