Configure, migrate, and verify projects

Coordinate npm Workspaces with TypeScript Project Builds

Link local npm workspaces, configure a TypeScript project build graph, and check declarations without confusing those steps with package resolution.

Editorial illustration for Connect npm Workspaces to TypeScript Project Builds

An npm workspace link and a TypeScript project build answer different questions. npm discovers local packages and links them under their declared package names. TypeScript project references describe relationships between projects for a multi-project build. If an API package depends on a shared package, record the dependency in npm and the build relationship in TypeScript. Neither configuration substitutes for the other.

This example stops at coordinating those two systems. It does not use a package-name TypeScript import as a test of type resolution. That is a separate question from whether npm made a workspace available or TypeScript emitted its declarations.

Declare the workspaces

Start with a private root package and two package directories:

package.json
tsconfig.base.json
tsconfig.json
packages/
  shared/
    package.json
    tsconfig.json
    src/index.ts
  api/
    package.json
    tsconfig.json
    src/index.ts

Use this root package.json:

{
  "name": "workspace-build-example",
  "private": true,
  "workspaces": ["packages/*"],
  "scripts": {
    "build": "tsc --build"
  },
  "devDependencies": {
    "typescript": "^5.3.3"
  }
}

The glob identifies package directories, but each directory needs a manifest of its own. Save this as packages/shared/package.json:

{
  "name": "@example/shared",
  "version": "1.0.0",
  "main": "./dist/index.js",
  "types": "./dist/index.d.ts"
}

Start packages/api/package.json with a distinct name:

{
  "name": "@example/api",
  "version": "1.0.0",
  "main": "./dist/index.js"
}

From the root, install dependencies and inspect the local link:

npm install
ls -l node_modules/@example/shared

As the npm workspaces documentation describes, npm install links a declared workspace into the root node_modules. The link points to the package directory. Its presence establishes that npm discovered and linked the workspace, not that dist/index.js or dist/index.d.ts exists.

Record the consumer dependency

The root workspace glob does not say which package depends on which other package. Add the shared package by name to the API workspace:

npm install @example/shared -w @example/api
cat packages/api/package.json

npm documents npm install <package-name> -w <workspace-name> for adding a dependency to a selected workspace. In the inspected API manifest, look for a dependencies entry for @example/shared. For example, it might appear as:

{
  "name": "@example/api",
  "version": "1.0.0",
  "main": "./dist/index.js",
  "dependencies": {
    "@example/shared": "^1.0.0"
  }
}

Check the version range npm actually wrote rather than relying on the illustrative value. This manifest check establishes the declared package dependency. It says nothing about whether the shared project's TypeScript files have been built.

Describe the build relationship to TypeScript

For this illustrative CommonJS build, put common compiler options in tsconfig.base.json:

{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "strict": true,
    "target": "ES2020",
    "module": "CommonJS",
    "moduleResolution": "node"
  }
}

Give each project its own source and output directories. In packages/shared/tsconfig.json:

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "rootDir": "src",
    "outDir": "dist"
  },
  "include": ["src"]
}

In packages/api/tsconfig.json, add a reference to the shared project:

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "rootDir": "src",
    "outDir": "dist"
  },
  "include": ["src"],
  "references": [{ "path": "../shared" }]
}

Make the root tsconfig.json a solution that lists both projects without listing source files of its own:

{
  "files": [],
  "references": [
    { "path": "packages/shared" },
    { "path": "packages/api" }
  ]
}

Here, the API manifest records an npm dependency while the API tsconfig.json records a TypeScript project reference. The root tsc --build command operates on the configured project graph, not on packages/*. The multi-project walkthrough uses composite projects, package-level output directories, references, and build mode in this arrangement.

Build the shared project, then inspect its output

Give the shared project a small declaration to emit. Save this as packages/shared/src/index.ts:

export interface User {
  id: number;
  name: string;
}

For an independently buildable API project in this example, packages/api/src/index.ts can contain:

export const serviceName = "api";

Build the shared project and inspect its output before treating it as built:

npx tsc --build packages/shared
ls packages/shared/dist

Look for index.d.ts. Its presence is a check that declaration output was produced for the shared source. It is not the workspace link: the link identifies the package directory, whereas the declaration is a file generated by the TypeScript build. The shared manifest's types value names the intended output path, but neither the manifest nor the file's existence demonstrates that an API import by package name resolves to it.

Finally, exercise the root project graph and recheck the two npm facts separately:

npm run build
ls -l node_modules/@example/shared
cat packages/api/package.json
ls packages/shared/dist/index.d.ts

A completed root build checks these configured TypeScript projects. The link check establishes npm linking, the manifest check establishes the recorded dependency, and the declaration check establishes emitted output. None of those checks, alone or together, tests a package-name TypeScript import or proves that Node can load an emitted entry point. Keep those questions separate when adding actual cross-package imports or runtime execution.

Find a note

Search by topic, title, or keyword.