Sub topic 7
Importing across project boundaries
This chapter assumes you have read the earlier chapters on composite, declaration outputs, and the references field. You should be comfortable with ES module imports and with how NodeNext resolution treats file extensions.
Once app lists core in references, the compiler treats core differently from the rest of your files. This chapter covers what the compiler does when it meets an import that crosses that boundary, the errors you can hit, and a runtime problem the compiler does not warn you about.
What the compiler does with a cross-project import
Here are the two files from the running example.
export function greet(name: string): string {
return `Hello, ${name}!`;
}This is packages/core/src/greet.ts. The app project imports it by relative path.
import { greet } from "../../core/src/greet.js";
console.log(greet("project references"));This is packages/app/src/main.ts. The import points at core/src/greet.ts, which is a source file. Without references, the compiler would load that file, parse it, and type-check it as part of app. With the reference, the compiler notices that the resolved file belongs to the referenced project core. It then substitutes that project's output file, packages/core/dist/greet.d.ts, and reads the declarations from it. The source file is not re-checked.
The substitution uses the referenced project's own rootDir and outDir settings to compute the output path. That is why those two options must be set deliberately. The compiler maps src/greet.ts to dist/greet.d.ts by swapping the rootDir prefix for the outDir prefix.
The left branch matters for diagnosis. If you forget the reference, a cross-project import often still compiles. The compiler pulls greet.ts into app, and then you get one of two problems. Either the file falls outside app's rootDir (error TS6059), or you lose the isolation and speed that references are meant to give you. A reference turns this accidental inclusion into a deliberate, checked dependency.
What a referenced project must provide
The compiler can only substitute outputs if the referenced project meets some conditions.
- The project must set
"composite": true. If it does not, the compiler reports TS6306:Referenced project '/repo/packages/core' must have setting "composite": true. - The imported file must be matched by the project's
includeorfiles. If it is not, the compiler reports TS6307:File '/repo/packages/core/src/extra.ts' is not listed within the file list of project '/repo/packages/core/tsconfig.json'. Projects must list all files or use an 'include' pattern. - The output must exist. A fresh clone has no
distdirectory, so buildingappalone fails with TS6305.
# On a clean checkout, type-checking app alone fails
tsc -p packages/app
# error TS6305: Output file '/repo/packages/core/dist/greet.d.ts'
# has not been built from source file '/repo/packages/core/src/greet.ts'.
# Build mode builds core first, then app
tsc --build packages/apptsc -p checks one project and never builds its dependencies. tsc --build reads references and builds the dependencies first. TS6305 almost always means you used the wrong command or the build order is stale.
The three cross-project errors
| Code | Message (shortened) | Usual cause | Fix |
|---|---|---|---|
| TS6305 | Output file has not been built from source file | The referenced project has no `.d.ts` output yet, or the output is older than the source | Run `tsc --build` instead of `tsc -p` |
| TS6306 | Referenced project must have setting "composite": true | The target of `references` is not a composite project | Add `"composite": true` to the referenced tsconfig |
| TS6307 | File is not listed within the file list of project | The imported file is outside the referenced project's `include` or `files` | Widen `include`, or move the file inside the project |
TS6307 also appears in the other direction. If app imports core/src/internal/helpers.ts and your core project has "include": ["src/index.ts"], then helpers.ts is not part of core. Under composite rules every file the project uses must be listed explicitly, so a transitive import does not make a file part of the project.
A gotcha: relative imports type-check but do not run
The example above has a problem that the compiler does not report. Look at what tsc emits for main.ts.
import { greet } from "../../core/src/greet.js";
console.log(greet("project references"));The compiler does not rewrite import specifiers. The emitted file is packages/app/dist/main.js. From that location, ../../core/src/greet.js resolves to packages/core/src/greet.js. That file does not exist, because core emits to packages/core/dist/greet.js. Type-checking passes, and running node packages/app/dist/main.js fails with ERR_MODULE_NOT_FOUND.
Relative paths into another project's src directory are therefore fine for demonstrating how references work, but they are a poor choice for code that has to run. The usual fix is to import by package name and let the module system find the compiled output.
Importing by package name
With npm workspaces, each project is a package. The root package.json declares the workspaces.
{
"name": "acme-monorepo",
"private": true,
"workspaces": ["packages/*"]
}The core package declares its entry point and points types at the declaration output.
{
"name": "@acme/core",
"version": "1.0.0",
"type": "module",
"exports": {
".": {
"types": "./dist/greet.d.ts",
"default": "./dist/greet.js"
}
}
}The app package depends on core by name. It also sets "type": "module", so that NodeNext treats its .ts files as ES modules.
{
"name": "@acme/app",
"version": "1.0.0",
"type": "module",
"dependencies": {
"@acme/core": "*"
}
}After npm install, the workspace links node_modules/@acme/core to packages/core. Now main.ts imports by name.
import { greet } from "@acme/core";
console.log(greet("project references"));Resolution now goes through node_modules and the exports map to packages/core/dist/greet.d.ts. That file is the output of a referenced project, so the compiler uses it as it did before. At runtime, Node resolves the same specifier to dist/greet.js, which exists after the build. The references entry in app's tsconfig still does its job. It guarantees that core is built before app, and it lets editors map the output back to source.
Without the references entry, the import would still resolve, but tsc --build would not know that app depends on core. It might check app before core has been built.
Choosing between the two styles
| Row | Relative path into `src` | Package name through workspaces |
|---|---|---|
| Type-checks with references | Yes | Yes |
| Runs after build | No, the emitted path points at `src` | Yes, Node resolves through `exports` |
| Setup cost | None | A `package.json` per project and an install step |
| Matches how external consumers import it | No | Yes |
Use relative imports only inside one project. Across projects, prefer package names.
Declaration maps and what you see in the editor
Because core sets declarationMap, each .d.ts file has a .d.ts.map beside it. When you ask the editor to go to the definition of greet, it lands on greet.d.ts, and the map redirects you to greet.ts. Without the map, you would stop at the generated declaration file. A later chapter covers editor behavior in more detail.
Edge cases
- Type-only imports.
import type { X } from "@acme/core"is erased at emit, so it works even when the runtime path is wrong. This can hide the runtime problem described above until a value import appears. - Stale outputs. If you edit
core/src/greet.tsand runtsc -p packages/app, the compiler reads the oldgreet.d.ts. Your change is invisible until you rebuildcore.tsc --buildhandles this because it compares timestamps and the.tsbuildinfodata first. - Files outside
rootDir. A referenced project cannot import files from outside its ownrootDir. Move shared code into its own project, and reference that project. - Path mappings. If you use
pathsto alias a project name to itssrcdirectory, the compiler still maps the resolved file to the referenced project's output. However, the emitted JavaScript keeps the alias, so you need a bundler or a runtime loader to resolve it.
Exercise
In a clean checkout, tsc -p packages/app fails with TS6305 about packages/core/dist/greet.d.ts, even though app correctly lists core in references. Why does it fail, and what command fixes it? After the fix, node packages/app/dist/main.js still fails with ERR_MODULE_NOT_FOUND because main.ts imports ../../core/src/greet.js. Why, and how do you fix that?
Show answer
tsc -p checks only app and does not build its references. The compiler maps the import to core/dist/greet.d.ts, which does not exist yet, so it reports TS6305. Running tsc --build packages/app builds core first and then app. The runtime failure happens because tsc does not rewrite import specifiers. From packages/app/dist/, the path ../../core/src/greet.js resolves to packages/core/src/greet.js, but core only emits to packages/core/dist/. The fix is to import by package name, @acme/core, with npm workspaces and an exports map in core's package.json that points types at dist/greet.d.ts and default at dist/greet.js.