Skip to content

Reusing JavaScript

Reuse source code through an explicit package boundary. Copying files between applications is fast once and expensive every time the shared behavior changes; a local workspace package makes ownership, versioning, and tests visible.

Start with a workspace package

json
{
  "dependencies": {
    "@example/shared": "workspace:*"
  }
}

The workspace protocol uses the local package during development and protects the repository from accidentally resolving an unrelated registry version.

Export a narrow API

ts
export function normalizeName(value: string) {
  return value.trim().toLowerCase()
}

Keep the public entry point small. A shared package should expose behavior that has a real consumer, not a collection of unrelated helpers that happen to be nearby.

Choose the right reuse level

NeedBetter boundary
Shared runtime behaviorA versioned package
Shared build and lint policyA preset package or managed asset
Shared application featureA feature package with clear ownership
One-off project setupA template, not a dependency

Keep Reading