In a web build in a workspace monorepo, why can resolving a linked package to its real path load a second copy of a shared dependency, and how do you fix it?
answer
- a workspace link is a symlink
- identity follows the real path
- lookups climb from the real folder
- the package's own node_modules first
- one physical copy, or one root
basics
~20 sAfter following the link, the shared package's files resolve bare imports from its real folder, where a copy installed inside the package, if any, is found first. Keep one physical copy, or resolve that dependency from the app root.
solid answer
~50 sA workspace links `@shop/ui` into the app's `node_modules` as a **symlink** to `packages/ui`. By default the resolver follows the link and identifies the files by their **real path**, so a bare import inside `packages/ui/src/Button.js` climbs from `packages/ui`. If that package has its own copy of the component framework, installed for its tests or stories, that copy is found first, and the bundle now holds two instances of the framework even at the same version: state one copy registers is invisible to the other. Keeping link paths instead changes the failure, not the cause: the link path still contains the package's own `node_modules`, and one real file reached through two links becomes two modules. Fix the installation so one copy exists, declare the framework a peer dependency of the shared package, or have the build resolve that dependency from the app's root for every importer.
code
bash · 10 linesrepo/
apps/store/
node_modules/
ui-framework/ # copy A, found from apps/store/src
@shop/ui -> packages/ui # workspace symlink
src/main.js # import 'ui-framework'; import '@shop/ui'
packages/ui/
node_modules/
ui-framework/ # copy B, installed for the package's own tests
src/Button.js # import 'ui-framework' climbs from here: copy Bgo deeper
Recall that a workspace links a local package into node_modules with a symlink, and that the build usually follows it to the package's real folder.
Explain how the bare-import climb starts from the importing file's real location, so a linked package's imports can find a copy the app never uses.
Diagnose the two-instance bug from its symptoms, prove it with resolved paths, and choose between peer dependencies, a single install and resolving the singleton from the app root.
Set the workspace policy (peers for singletons, one version per dependency, built or source consumption) so no team has to rediscover this bug in their own app.
## What a workspace link is In a **workspace monorepo**, packages that live in the same repository depend on each other without being published. The package manager makes that work with a **symbolic link** (symlink): `apps/store/node_modules/@shop/ui` is not a copy but a filesystem pointer to `packages/ui`. To a resolver, the package is found by the usual `node_modules` climb, and then a question arises: is the file `apps/store/node_modules/@shop/ui/src/Button.js` or `packages/ui/src/Button.js`? ## Real path or link path Build tools answer that with one setting, and by default they **follow the link**: | Choice | File identity | Where the package's own bare imports are looked up | |---|---|---| | Real path (the usual default) | `packages/ui/src/Button.js` | climbing from `packages/ui/src` | | Link path (opt-in) | `apps/store/node_modules/@shop/ui/src/Button.js` | climbing from inside the app's `node_modules` | Following links has a good reason: the same package linked from two places is still one module. Its cost is that the shared package's dependencies are resolved **from its own folder**, not from the app that imports it. ## How the second copy appears 1. App code imports the component framework; the climb from `apps/store/src` finds copy A in `apps/store/node_modules` (or the root `node_modules`). 2. App code imports `@shop/ui`; the link is followed to `packages/ui`. 3. `packages/ui/src/Button.js` imports the framework; the climb from `packages/ui/src` reaches `packages/ui/node_modules` first, where the package's own development install placed copy B. 4. Two files, two nodes, two instances. Nothing reports an error. The version may be identical, so a version-focused check looks clean. The symptom is behavioural: a component framework, a router or a store usually keeps **module-level state** (a registry, the current renderer, a context object), and a value created by one copy is unknown to the other. Errors read like "must be used inside a provider" while the provider is plainly there, or an `instanceof` check fails for an object of the right class. ## The opposite failure: cannot resolve The same rule produces a second symptom when the layout is different. If the framework is installed only beside the app, in `apps/store/node_modules`, and neither in the package nor at the root, the climb from `packages/ui/src` never passes through the app's folder. The build then fails with "could not resolve" for an import inside `Button.js`, although the app plainly has the dependency. Both symptoms come from one fact: after following the link, the package's files resolve their imports from where they really are. ## Why keeping link paths does not save you Switching the resolver to link paths moves the lookup start to `apps/store/node_modules/@shop/ui`, but that folder is the link, and through it `node_modules/@shop/ui/node_modules/` is the package's own `node_modules`. Copy B is still found first. The setting also adds a failure of its own: one real file reached through two different link paths becomes two modules. It suits a few edge cases, not this one. ## Fixes, from most to least structural - **One physical copy.** Make the shared package declare the framework as a **peer dependency**, so its consumers supply it, and align versions so the package manager installs it once at the root. If the package keeps a nested copy for its own tests, the next fix may still be required. - **Resolve the singleton from one place.** Have the build resolve that dependency from the app's root for every importer, through a deduplication or alias setting. This works whatever the install layout. - **Remove the nested install.** If nothing in the package really needs a private copy, dropping it from the package's own dependencies removes copy B at the source. ## Confirming it - List the modules in the build and search for the framework's entry file; two absolute paths mean two copies. - Log a module-level value from both sides, such as an identity check on an exported object. - Check `packages/ui/node_modules` for a nested install. - After the fix, rebuild and repeat the path search: a single absolute path for the framework's entry file is the proof that only one instance ships. Reading a size report for two different versions of one package is a related but separate task; here the copies may be byte-for-byte identical.
- Why is a peer dependency the right declaration for a component framework in a shared workspace package?A peer dependency says the package needs the framework but expects its consumer to provide it, so the package manager does not install a private copy for it in a consumer's tree. That keeps one instance, which the framework's module-level state requires. The package still lists the framework as a development dependency for its own tests, which is why the build-side fix of resolving it from the app root is often needed as well.
- What else changes when a build treats a linked package as source rather than as an installed dependency?Its files go through the app's own transforms, so its syntax must be something the app's build can process, and edits to it show up without a separate build of the package. Its imports are resolved from its real folder, as for the framework above, so its dependencies must be installed where that climb can find them. Its own build settings, such as the targets it would have compiled for, no longer apply.
- Two copies only happen when two different versions are installed.
- A linked package's imports are always resolved from the app's folder.
- Keeping symlink paths guarantees a single copy of every dependency.
- Two instances of the same version behave like one at run time.
- A peer dependency alone stops a package's development copy being found.