Skip to content

/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

bash
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 tsc

Output · 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

bash
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:

bash
npm install lodash
npm install --save-dev @types/lodash   # only for packages without bundled types

Checklist

SymptomCheck
Package import failsnpm ls NAME, and whether node_modules/NAME has a types or exports entry
Relative import failsThe path from the importing file; with nodenext, use ./file.js
Alias (@/...) failscompilerOptions.paths in tsconfig.json and the bundler alias
Works in the editor, fails in CIWhich tsconfig the CI command uses (tsc -p)

Sources