Hacker News new | ask | show | jobs
by danShumway 2833 days ago
Put your content inside of a nested node_modules folder and you won't have this problem anymore.

I don't think node_modules is perfect, and I get why it gets hate, but IMHO the algorithm is actually kinda nice for nesting packages.

If you set up your folders like so:

----

src--->node_modules--->utils--->helper.js

src--->main.js

----

You can require your helper in main.js with a simple ``require('utils/helper.js');``

What's nice is that you can't do the reverse. So your helper doesn't get the same access to your main.js. I use this a lot for testing - it means that my tests can require my components using the same syntax that I use in normal code, but those components don't have special access to the tests.

A big "aha" moment for me with node_modules was figuring out that it's an entirely separate system from npm. It's not trying to build a flat package structure; it's trying to make it easy to on-the-fly set up packages from anywhere.

Edit: example - https://gitlab.com/dormouse/dormouse/tree/master/src

I've also gotten into the habit of checking my installed packages into source for projects that I don't expect users to rebuild (ie. websites, games, etc...). That's a much longer conversation, but it mitigates a large number of issues with node_modules on long-lived projects.

2 comments

> A big "aha" moment for me with node_modules was figuring out that it's an entirely separate system from npm.

I wish this was true. My workflow from back in the early days of node has always been to `npm install` external dependencies, then `npm link` (symlink) the dependencies I'm working on. But npm >= v5 removes such symlinks if I `npm install` anything afterwards. I spend a significant amount of my time re-linking what npm install unlinked.

Usually when I hit a problem like this it's because I'm things have moved on and I'm doing something wrong. But when an npm developer says "consider npm as it is to be broken" and closes the issue [1], I'm not so sure.

[1] https://github.com/npm/npm/issues/17287#issuecomment-4008339...

To repeat: a big "aha" moment for me with node_modules was figuring out that it's an entirely separate system[0] from npm.

Node's module resolution has nothing to do with npm. Npm is a package repository and installer built on top of node_modules. The reason your system broke is because npm cleared out your node_modules folder as part of its install. And from the sound of things on your linked issue, the devs are entirely aware of the problems this behavior causes and are planning to fix it.

An interesting exercise that I highly encourage people to do if they're finding this weird is to take a weekend and build their own version of npm just to demystify what's going on with it. It's not that hard to do, Node gives you all the tools you need - in its simplest form you need to curl whatever packages you want to install, and stick them in a node_modules folder. Then you need some way to track which packages you've downloaded. Node handles all the rest.

That npm occasionally breaks Node behavior is bad - I've been on the wrong end of those regressions as well[1] and it was super frustrating. But that doesn't really have anything to do with Node, it just means the npm team needs to test their releases more.

[0]: https://nodejs.org/api/modules.html#modules_loading_from_nod...

[1]: https://github.com/npm/npm/issues/18942

That’s actually a pretty good idea and something I had not realized was possible.