Cannot Find Module or Its Type Declarations: Trace It

error TS2307: Cannot find module 'x' or its corresponding type declarations means TypeScript’s resolver looked for a file to match that import and found none: no .ts, no .d.ts, no JavaScript. Run tsc --traceResolution, find the block for the import, and it lists every place the compiler looked and the step where it gave up.

We built a folder of imports that each fail a different way and ran typescript 7.0.2, 6.0.3 and 5.9.3 over it on 2 December 2026. Five different mistakes produced the same TS2307 line on all three versions, and the trace told them apart.

How do you read the resolution trace?

traceResolution is a compiler option that, in the TSConfig reference’s words, makes TypeScript “print information about its resolution process for each processed file”. Our twelve small files produced 279 lines on 7.0.2, so send the output to a file and pull out one import:

npx tsc --noEmit --traceResolution > trace.txt
awk "/Resolving module 'unbuilt'/,/^======== Module name/" trace.txt

unbuilt is our stand-in for a workspace package whose package.json points at a dist folder that has not been built. Its block runs to 39 lines. These are the six that matter, with our scratch path shortened to /app:

======== Resolving module 'unbuilt' from '/app/src/unbuilt.ts'. ========
Found 'package.json' at '/app/node_modules/unbuilt/package.json'.
Matched 'exports' condition 'types'.
Using 'exports' subpath '.' with target './dist/index.d.ts'.
File '/app/node_modules/unbuilt/dist/index.d.ts' does not exist.
======== Module name 'unbuilt' was not resolved. ========

Every block opens with the import and closes with a verdict. In between, look for the lines that name a package.json or a file. Here the package was found, its exports map was followed to ./dist/index.d.ts, and that file is not there. Nothing is wrong with the import or the tsconfig. The dependency needs building first, and once we created the two files in dist the error went away.

What causes “Cannot find module or its corresponding type declarations”?

One line in each trace block separates the five.

The block containsWhat it meansThe fix
No Found 'package.json' at lineThe package is in no node_modules folder above the importing fileInstall it where it is imported
Export specifier './lib/extra.js' does not exist in package.json scopeThe package has an exports map and the subpath is not in itImport an entry the package exports
Using 'exports' subpath '.' with target './dist/index.d.ts', then does not existThe entry point has not been builtBuild the dependency
Loading module '@/math.js' from 'node_modules' folderAn alias with no paths entry is looked up as a packageAdd paths
File '.../src/logo.d.svg.ts' does not existA non-code file has no declarationDeclare the module

If the block ends in was successfully resolved and the import still fails, the error is not TS2307. TypeScript found JavaScript with no types beside it and reports TS7016: Could not find a declaration file for module, whose message asks for an @types package or a declaration.

Why does an installed package still fail?

Rows two and three are packages that sit in node_modules and still cannot be found, and both come from the exports field. The Modules reference gives the rule in one sentence: “the presence of "exports" prevents any subpaths not explicitly listed or matched by a pattern in "exports" from being resolved”. Our sealed package has lib/extra.js on disk and lists only . in its map. TypeScript refused sealed/lib/extra.js, and so did Node 24.12.0, which threw ERR_PACKAGE_PATH_NOT_EXPORTED for the same import, as its packages documentation says it will. Getting the import past the compiler would leave Node to refuse it at run time.

On 5.9.3 a package can also fail because the compiler never reads the map. "module": "commonjs" with no moduleResolution set used the node10 algorithm, which the Modules reference marks as not supporting exports, so a package described only by an exports map failed with TS2307. A second line said its types “could not be resolved under your current ‘moduleResolution’ setting” and named node16, nodenext and bundler as replacements. Set one of them. The same tsconfig resolved the package on 6.0.3 and 7.0.2, where the trace reports Bundler as the default. Writing "moduleResolution": "node10" out in full is a deprecation error on 6.0.3 (TS5107) and a removed option on 7.0.2 (TS5108).

A map can also be the package author’s mistake. Our split-types package keeps its types in a different folder from its JavaScript, and its exports map has no types condition. Importing it gave TS7016 with a note that begins “There are types at” and ends by saying the library “may need to update its package.json or typings”. Adding a types condition to the map cleared it in our copy. In someone else’s package that is a bug report. If the package is yours, our tsup to tsdown migration post covers generating the map and checking it.

How do you fix a path alias or an asset import?

The trace for @/math.js shows TypeScript searching node_modules for it, because nothing has said that @/ means src/. A paths entry says so:

{
  "compilerOptions": {
    "module": "nodenext",
    "strict": true,
    "noEmit": true,
    "paths": { "@/*": ["./src/*"] }
  },
  "include": ["src"]
}

The TSConfig reference resolves paths targets “relative to the baseUrl if set, or to the tsconfig file itself otherwise”. 7.0.2 rejects baseUrl as a removed option, so write the targets relative to the tsconfig. The same page warns that paths “does not change how import paths are emitted by tsc”, so whatever bundles or runs the code needs the same mapping.

If the alias is configured and one command still reports it, check how that command calls the compiler. On 5.9.3, tsc --noEmit src/alias.ts skipped the tsconfig and reported TS2307 for an alias that tsc -p . resolved. A pre-commit hook that passes staged file names to tsc does the same thing. 6.0.3 and 7.0.2 stop with TS5112 instead, which says the config “will not be loaded if files are specified on commandline”.

For ./logo.svg the trace shows the compiler looking for logo.d.svg.ts beside the image. The fix is one wildcard declaration in a .d.ts file that the tsconfig includes:

declare module "*.svg" {
  const url: string;
  export default url;
}

Keep that file free of top-level import and export statements. With export {}; added as its first line, the same declaration stopped working and TS2307 came back on all three versions. The Modules reference explains it: once the file “is a module”, the syntax “becomes a module augmentation” and no longer declares a new module.

Vite ships these declarations in vite/client, and "types": ["vite/client"] cleared the same error for us with vite 8.4.1 on 7.0.2.

What changed in TypeScript 6 and 7?

Two defaults moved in 6.0, and either can put this error or a near neighbour into a project that compiled cleanly on 5.9.

types now defaults to []. Before 6.0, every package under node_modules/@types was loaded whether you named it or not. The TypeScript 6.0 release notes list “Cannot find module ’…’ or its corresponding type declarations” among the errors to expect, and we reproduced it: a wildcard module declared inside an @types package was found by 5.9.3 and reported as TS2307 by 6.0.3 and 7.0.2 until we named the package in types. Node’s built-ins gave a different code. With @types/node installed and not listed, import { readFileSync } from "node:fs" produced TS2591, “Cannot find name ‘node:fs’”, followed by an instruction to add node to the types field.

noUncheckedSideEffectImports is on by default as well. import "./styles.css" passed silently on 5.9.3 and is TS2882 on 6.0.3 and 7.0.2, “Cannot find module or type declarations for side-effect import”. A declare module "*.css"; line cleared it. Our TypeScript 7 migration notes cover the other tsconfig changes.

What would we do first?

Run the trace before changing anything, and change the thing its block names.

TS7016’s own message suggests a shorthand declaration, declare module 'name';, and the same line silences TS2307. We tried it on the missing package and on the unbuilt one: the error disappeared on all three versions, and Node still threw ERR_MODULE_NOT_FOUND when asked to import either. For an asset that a bundler handles, a declaration is the right fix. For a package, it hides a file that is not there.

We would skip the trace for TS7016 and TS2591, whose messages already say what to install or list. And we ran tsc on the command line, not an editor, so we cannot say what an editor-only error needs.

We write TypeScript in strict mode across our production work, and we take on existing TypeScript projects that have collected disabled checks and loose configuration. Our TypeScript development page describes that work.

Need this built properly?

Get in touch

Hack the planet!