/errors/typescript/ts2307-cannot-find-module
TS2307: Cannot find module 'x' or its corresponding type declarations
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · v24.20.0updated
- tool
- TypeScript
- exit code
- 2
- versions
- TypeScript 4.x – 6.x
- os
- any
error TS2307: Cannot find module 'x' or its corresponding type declarations. means the TypeScript compiler could not resolve an import to a .ts file, a .d.ts declaration or a package with types. Either the module does not exist where TypeScript looks (not installed, wrong path), or it exists but ships no types. Install it (plus @types/x if needed) or fix the path.
Reproduce it
npm init -y >/dev/null
npm install --silent --no-audit --no-fund typescript@6 >/dev/null 2>&1
cat > tsconfig.json <<'EOF'
{ "compilerOptions": { "module": "preserve", "moduleResolution": "bundler", "strict": true, "noEmit": true }, "include": ["*.ts"] }
EOF
cat > index.ts <<'EOF'
import leftPad from "left-pad-x";
import { helper } from "./utils";
console.log(leftPad, helper);
EOF
npx tscOutput · exit 2
index.ts(1,21): error TS2307: Cannot find module 'left-pad-x' or its corresponding type declarations. index.ts(2,24): error TS2307: Cannot find module './utils' or its corresponding type declarations.
Both imports fail: the package is not installed, and ./utils.ts does not exist.
Fix: provide the module or its types
npm init -y >/dev/null
npm install --silent --no-audit --no-fund typescript@6 >/dev/null 2>&1
cat > tsconfig.json <<'EOF'
{ "compilerOptions": { "module": "preserve", "moduleResolution": "bundler", "strict": true, "noEmit": true }, "include": ["*.ts"] }
EOF
cat > index.ts <<'EOF'
import leftPad from "left-pad-x";
import { helper } from "./utils";
console.log(leftPad, helper);
EOF
echo 'export const helper = 1;' > utils.ts
echo 'declare module "left-pad-x";' > vendor.d.ts
npx tsc && echo "tsc: no errors"Output
tsc: no errors
For a real package, the usual fix is:
npm install lodash
npm install --save-dev @types/lodash # only for packages without bundled typesChecklist
| Symptom | Check |
|---|---|
| Package import fails | npm ls NAME, and whether node_modules/NAME has a types or exports entry |
| Relative import fails | The path from the importing file; with nodenext, use ./file.js |
| Alias (@/...) fails | compilerOptions.paths in tsconfig.json and the bundler alias |
| Works in the editor, fails in CI | Which tsconfig the CI command uses (tsc -p) |
Sources
Related pages
config · typescripttsconfig moduleResolution: bundler, nodenext, node16moduleResolution tells tsc how to find imports. TypeScript 7 accepts bundler, node16 and nodenext; node10 and classic were removed. Valid pairs with module.config · typescripttsconfig strict: what "strict": true enablesThe strict option in tsconfig.json enables noImplicitAny, strictNullChecks and the other strict checks at once. It defaults to true since TypeScript 6.0.errors · typescripterror TS5108: Option 'moduleResolution=node10' has been removedTypeScript 7 removed moduleResolution node and node10. Use bundler for code built by a bundler, or nodenext with module nodenext for code that Node.js runs.