by Klathmon · 10y ago · 0 comments
When you `require` or `import` a file in node.js, it looks for a node_modules and looks for that name in there. If it can't find it there, it starts walking up the directory tree until it finds something it can use (to a point).
This is hardcoded and will be extremely difficult to change without a crazy amount of breaking.
The package manager is free to install however, but it needs to put things where the package-lookup can find them.
Essentially, all they need to do is:
1. leave the current behavior for backwards compatibility; then
2. provide a flag like npm -G that exposes the correct behavior as suggested in the grand parent of using the same path like SHARED_DIR/node_modules/NAME/VERSION for package imports and package management.
With time, newer npm versions will default to the correct behavior. For folks that need backwards compatibility, this would require explicitly setting a npm --compat flag or similar.
node loads modules in a given pattern. Changing that pattern would be global to your project, and would cause issues with tons of 3rd party tools.
the best possible scenario would be to introduce a "new_node_modules" type directory and change to the new system, then look in "new_node_modules" first, then the legacy "node_modules" next, but that's a ton of work, a ton of 3rd party tool breakage, and a lot of possibility for new bugs and breakage for not all that much benefit.
That's not to say it shouldn't be done at some point, just that there are much bigger areas that need to be addressed sooner in the node ecosystem.
And it's easy to say "even a naive attempt by the average developer" would do it better, but I really don't think they would have.
Still, the fact is that this is what we have, and complaining about what could have been isn't going to do anyone any good, improving the system will.