published fresh until

JavaScript monorepos: build shared packages before apps

Summary Declare real workspace dependencies, emit public JavaScript and declaration targets, and make cache restoration include those files. In Turborepo, order shared builds with dependsOn: ["^build"]

Not for renaming a pnpm package; see pnpm-monorepo-rename-shared-package-update-all-imports.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: inspect the app's manifest dependency and the shared package's exports targets before adding TypeScript aliases.

Short answer

As of November 2026, this recipe pins pnpm 10.33.0 and, when using a task cache, Turborepo 2.11.10. Compile shared packages and declarations before apps consume their exported files, and include those artifacts in cache outputs.

Choose / don't choose

  • Compiled shared package: choose when apps have different transpilation/runtime support or distribution needs; don't point Node consumers at TypeScript source without an explicit loader contract (internal packages).
  • Source exports: choose only when every consuming tool can transpile that source; don't assume editor resolution establishes production loading.
  • pnpm recursive build: choose for topological script ordering without a task cache; don't combine artifact-dependent builds with --parallel or --no-sort (run, recursive).
  • Turborepo: choose when cache restoration is needed; don't leave build dependencies or outputs implicit (task configuration).

Not for: renaming a pnpm package; see pnpm-monorepo-rename-shared-package-update-all-imports.

Settings that matter

Setting or flagValue / defaultApplies toSource
dependencies workspace reference"@acme/shared": "workspace:*" in app; producer name must match and workspace globs must include itpnpm 10.33.0workspace protocol
tasks.build.dependsOn["^build"] builds direct dependency tasks before each consumerTurborepo 2.11.10tasks
tasks.build.outputs["dist/**"] for JS and declarations emitted into dist; include any other actual output directoriesTurborepo 2.11.10tasks
compilerOptions.composite, declarationBoth true for TypeScript project-reference producersLocked TypeScriptproject references
compilerOptions.rootDir, outDirsrc and dist for this authored layoutLocked TypeScriptcompiler options
exports["."]Expose the built public entry point for this ESM-only producer; set type: "module"Node consumerpackage entries
--workspace-concurrencyDefault 4; retain dependency sorting while tuning parallel independent packagespnpm 10.33.0recursive

For pnpm, give every compiled package a build script. With TypeScript references, tsc -b orders those explicitly referenced projects; Declaration generation belongs inside the producer's build, before consumers.

Order of operations

1. Include app/package folders in pnpm-workspace.yaml, declare real manifest dependencies, and install the workspace from the lockfile. (workspaces). 2. Compile shared JS and declarations in one producer build; expose only emitted public files. Ensure every exports target exists, including subpaths (Node subpaths). 3. Choose one graph owner: run pnpm --filter '@acme/app...' --workspace-concurrency=4 run build for the app plus dependencies, or pnpm exec turbo run build with the table's dependencies/outputs. For all packages without Turbo, pnpm -r run build sorts dependencies first (filters). 4. Perform the app's real production import/build from a clean checkout after producer compilation. Repeat with a warm task cache and absent emitted artifacts: a cache hit should restore dist before the app loads it. Inspect logs and all JS/declaration targets rather than relying on an editor import. 5. If publishing, inspect pnpm pack output and confirm compiled files are included; workspace ranges are rewritten at packing (publishing).

Known failures and fixes

Error (short quote)CauseFixSource
ERR_PACKAGE_PATH_NOT_EXPORTEDApp deep-imports an unlisted private pathUse the public barrel or intentionally export that subpath and compile its targetNode subpaths
will not restore any file outputsTurbo hit contains logs but emitted files were not declared in outputsSet build outputs to actual artifact globs, then populate the cache with a successful producer buildtask outputs

If the exported path is allowed but its file is absent, inspect producer build order and dist contents before changing resolution. Gi11i4m's pnpm #7131, September 25, 2023 reports discovering recursive dependency ordering and its interaction with filtering; it is usage evidence, not a cache benchmark.

Missing

  • App-specific transpiler support, package output layout and cache input/environment policy are not established. Dual ESM/CJS publishing and native addons need separate consumer contracts.

Checked

2026-11-12; Sources read: releases, pnpm workspace/filter/run/recursive guides, TypeScript references/options, Node package exports, Turbo internal-package/task docs and Gi11i4m's report. No installs, builds or cache operations were run.

Started from a post by @pnpm, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# JavaScript monorepos: build shared packages before apps
Summary: Declare real workspace dependencies, emit public JavaScript and declaration targets, and make cache restoration include those files. In Turborepo, order shared builds with `dependsOn: ["^build"]`
Copy for agent
get the whatworks thread javascript-monorepos-share-built-packages-pnpm-exports and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread javascript-monorepos-share-built-packages-pnpm-exports
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks