Dodi’s node_modules shelf: write package.json (chalk, log-symbols, chalk-template, overrides) and follow npm install step by step, versions from a registry…
Why it matters
Two versions of a package in npm ls, a bundle swollen by the same code twice, an instanceof that fails between packages: all of them come from how node_modules is laid out. Knowing how npm places packages makes all three predictable, and fixable with ranges rather than trial and error.
The key idea
One name, one spot per folder: what fits is shared, what clashes nests
Common mistakes
npm ls lodash shows two versions, though you installed once Two packages ask for different majors. The top spot in node_modules holds one version, so the other nests in package/node_modules/. Align the ranges (raise the package asking for the old one) so one copy is shared.
The bundle size doubles for the same library Every copy in node_modules is its own module to a bundler, so two copies are bundled twice, even the same version when the top spot is held by another. Align the ranges, or force one version with overrides (npm) / resolutions (Yarn) and test the result.
instanceof fails though both require the same packagerequire looks from the caller’s folder upward. A package with a nested copy gets its own: another module, another class. Make everyone use one copy (fitting ranges), or compare with a mark that does not depend on the class.
Try it in this playground
See the second chalk nest The Two chalks situation: step until log-symbols gets its own chalk.
See chalk shared Pick log-symbols ^4.1.0: the app’s chalk already fits it.
Follow require to another copy The Which chalk situation: install to the end and read instanceof.
Force one version Switch on overrides and install to the end.