Skip to content

Instantly share code, notes, and snippets.

@ksherlock
Last active March 14, 2016 17:33
Show Gist options
  • Save ksherlock/d28d436c2b56d9a38f0c to your computer and use it in GitHub Desktop.
Save ksherlock/d28d436c2b56d9a38f0c to your computer and use it in GitHub Desktop.
A better node_modules layout

All packages (top-level and dependencies) go in the __bikeshed__/ directory where they are versioned.

node_modules/__bikeshed__/[email protected]/
node_modules/__bikeshed__/[email protected]/
node_modules/__bikeshed__/[email protected]/
node_modules/__bikeshed__/[email protected]/

When you install a package, it also creates a top-level entry which redirects to a specific version in the __bikeshed__/ directory. This could be done with softlinks or auto-generated files. (require(x) will load for x.js).

node_modules/add_two_numbers.js

// auto-generated_file -- do not edit.
'use strict';
module.exports = require('__bikeshed__/[email protected]');

--or--
node_modules/add_two_numbers -> __bikeshed__/[email protected]

packages in the __bikeshed__/ directory also have a node_modules/ directory that redirect to a specific version:

node_modules/__bikeshed__/[email protected]/node_modules/lodash.js

// auto-generated_file -- do not edit.
'use strict';
module.exports = require('__bikeshed__/[email protected]');

--or--
node_modules/__bikeshed__/[email protected]/node_modules/lodash -> ../../[email protected]

This layout eliminates duplicate packages and creates a smaller and flatter node_modules/ directory. It also makes version swapping painless since you only need to change one file, not an entire hierarchy. The only downside I can see is an extra require() call in the package stub. Using soft links would eliminate that issue.

The end!

@ksherlock
Copy link
Author

And.... pnpm does that. 99.9% of node doesn't get it but once in a while you find something (like rollup or pnpm) that gets it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment