monorepo中跨包导入问题
Error is gone. Here’s the full explanation of everything at play:
Summary: TypeScript + package.json exports in monorepo
Why the error occurred
Your tsconfig.base.json has composite: true, which implies all source files must be under rootDir (defaults to the tsconfig’s directory). When TypeScript resolves @package/ui/Navigation via the exports field → ./src/Navigation/index.ts, that file lives in package/ui/, which is outside package/app/‘s rootDir. Hence the error.
Additionally, include: ["../ui/**/*.ts"] was directly pulling ui source into app’s program — a double violation.
How TypeScript resolves package.json exports
With moduleResolution: "Bundler" (your base config), TypeScript fully supports the exports field:
import {RouterLink} from '@package/ui/Navigation'- TS finds
@package/ui→ readspackage.json - Matches
exports["./Navigation"]→ usestypescondition →./src/Navigation/index.ts - Resolution succeeds
The issue was never about exports compatibility — it was about composite‘s rootDir enforcement.
What was changed
package/app/tsconfig.json:
- Overrode
composite: false,declaration: false,declarationMap: false,emitDeclarationOnly: false— app is a leaf Vite application, not a library, so it doesn’t need these - Removed
"../ui/**/*.ts", "../ui/**/*.tsx"frominclude— app should only compile its own source; cross-package types come throughexportsresolution - Removed
outDirsince app doesn’t emit anything (Vite handles bundling)
Root tsconfig.json:
- Removed
appfromreferences— withoutcomposite, it can’t participate intsc -b
The pattern for extensionless cross-package imports
Your current package/ui/package.json exports setup is correct for this:
1 | "./*": { |
- Wildcard
./*— handles extensionless file imports like@package/ui/SomeFile - Explicit
./Navigation— needed for directory-based exports (where the actual file isindex.tsinside a folder), because the wildcard won’t automatically append/index.ts
The rule: for any directory with an index.ts, add an explicit exports entry. Wildcards only work for direct file-to-file mapping.