From Copy-Paste to Package: Extracting a Shared Library
The same helper module lives in four services, each copy slightly different. Someone suggests turning it into a shared package. This deck outline is for the engineer making that case to their team. It covers when extraction is worth it, how to design the first version, and the costs people forget. Each slide has speaker notes you can read from or rewrite.
Part one: should we? (slides 1 to 4)
- The problem in one picture. Notes: show four boxes, one per service, each containing its own copy of the module with small differences highlighted. Describe one real bug that was fixed in one copy but not the others, if you have one. Do not invent one.
- What a shared package gives us. Notes: one place to fix bugs, one set of tests, explicit versions so each service can upgrade on its own schedule.
- What it costs. Notes: a release process, a changelog, compatibility discipline and a named owner. Shared code that nobody owns becomes the worst code in the company.
- The rule of three. Notes: a common heuristic is to wait until the same logic appears in three places before abstracting it. Two copies may still be diverging for good reasons.
Part two: designing version one (slides 5 to 9)
- Find the common core. Notes: diff the copies. What is genuinely shared becomes the library; what differs becomes configuration or stays in the service.
- Keep the public API small. Notes: export only what at least two consumers need today. Every export is a promise; adding later is easy, removing later is a breaking change.
- Choose dependencies carefully. Notes: a shared library's dependencies become every consumer's dependencies. Prefer the standard library; declare framework-level dependencies as peers if your ecosystem supports that.
- Start at 0.x or 1.0? Notes: semver.org says 0.y.z is for initial development where anything may change. If several production services will depend on it immediately, starting at 1.0.0 and honouring semver sends a clearer signal.
- The manifest. Notes: show the minimal manifest on screen.
{
"name": "@acme/retry",
"version": "1.0.0",
"type": "module",
"exports": { ".": "./dist/index.js" },
"files": ["dist"],
"license": "UNLICENSED",
"publishConfig": { "registry": "https://registry.example.internal/npm/" }
}
Illustrative only. UNLICENSED is the npm convention for proprietary code not offered under any licence.
Part three: rolling it out (slides 10 to 12)
- Migrate one service first. Notes: pick the simplest consumer, swap its copy for the package, and fix what breaks in the library, not in the service.
- Then the others, one at a time. Notes: each migration will reveal an assumption. Expect a few minor releases in the first weeks; that is healthy.
- Ownership and support. Notes: name the owning team, where issues go, and the policy for breaking changes, such as a deprecation warning in one minor release before removal in the next major.
Questions to prepare for
| Likely question | Short answer |
|---|---|
| "Why not a monorepo instead?" | A monorepo changes where code lives; you still need clear boundaries and an owner. The two are compatible. |
| "What if one service needs a special case?" | Add an option only if it is general; otherwise keep the special case in that service. |
| "Who fixes bugs at 2 a.m.?" | The service owner pins the previous version; the library owner fixes forward and publishes a patch during working hours. |
For the API design side in more depth, see the public API design course outline.