Report #79794
[bug\_fix] TS2307: Cannot find module '@org/shared' or its corresponding type declarations when using project references in a monorepo.
In the dependency package \(\`@org/shared\`\), ensure \`tsconfig.json\` has \`compilerOptions.composite: true\` and \`declaration: true\`. In the consuming package \(\`@org/app\`\), add a \`references\` array to its \`tsconfig.json\` pointing to the dependency: \`\{"references": \[\{"path": "../shared"\}\]\}\`. Use \`tsc --build\` \(or \`tsc -b\`\) to compile instead of \`tsc\`. Root cause: TypeScript's project references enforce a build order and ensure that dependencies emit declaration files \(\`.d.ts\`\) that the consuming project can use. Without \`composite: true\`, TypeScript does not treat the project as a valid dependency for \`tsc --build\`, and without the \`references\` array, the compiler does not know to build the dependency first or where to find its emitted types, leading to module resolution errors even if the source files exist.
Journey Context:
A developer migrates a large Node.js codebase to a pnpm monorepo with two packages: \`utils\` and \`api\`. The \`api\` package imports \`utils\` via a relative path \`import \{ helper \} from '../utils/src/index'\`. They run \`tsc\` inside \`api\` and encounter the error: "error TS2307: Cannot find module '../utils/src/index' or its corresponding type declarations." They verify the file exists. They try adding \`"paths": \{ "@utils/\*": \["../utils/src/\*"\] \}\` to \`api/tsconfig.json\`, but this fails because path mapping doesn't cross project boundaries for emitted files. They research monorepo patterns and discover TypeScript Project References. They create a \`tsconfig.json\` in \`utils\` with \`compilerOptions.composite: true\` and \`declaration: true\`. In \`api/tsconfig.json\`, they remove the manual path mapping and add a \`references\` array: \`\{"references": \[\{ "path": "../utils" \}\]\}\`. They change the import in \`api\` to use the package name if configured, or keep the relative path but ensure the reference is established. They run \`tsc --build\` from the monorepo root \(or from \`api\` with \`-b\`\). The compiler builds \`utils\` first, emitting \`.js\` and \`.d.ts\` files, then compiles \`api\` using those declarations. The TS2307 error is resolved. The developer now understands that project references enforce a build graph and separate the compilation of dependent packages.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T16:31:51.084437+00:00— report_created — created