Skip to content
This repository was archived by the owner on Mar 25, 2018. It is now read-only.
This repository was archived by the owner on Mar 25, 2018. It is now read-only.

My 2 cents on modules — v2 #20

Open
@leo

Description

@leo

I recently wrote something about how modules in node currently work (using NPM) and @Fishrock123 pointed me here, since the node repo probably wasn't the right place to post something like this.

I was basically asking why NPM doesn't simply put all packages in one place (like most package managers do it), because this method would make the whole thing a bit more continuous while keeping it less complex.

After I did some research, I also found out that NPM already introduced the "flat" feature in version 3.0.0, which simply puts all modules on one level within the node_modules directory (as long as there are no version incompatibilities, otherwise they'll be nested as before).

Since that already solves one of the problems I've mentioned earlier ("nesting inception"), I'm glad that they made such a decision. It's already going into the direction which I pointed out and moves all modules into one level.

Talking about the other things I've mentioned in my post: Why don't we go another step and move all modules into a single, global directory? I mean, they're already in a single one (for each project).

Although I've already read things like this, nobody really pointed out what's the problem here (and why they don't want to use the method of keeping everything globally). Everywhere I look, I just see something like this:

It's not going to happen, because it's a terrible idea that causes more problems than it solves.

But ... why?

I'm assuming that one problem might be the fact that some modules don't work with the latest version of a module which they depend on. That's true, right? And if that's the main reason why you don't want to switch to a different setup, why don't we simply do it like this:

  1. Let's create a global directory for all node modules.

  2. Within this directory, there are sub-directories (a.k.a. "modules")

  3. And now (to solve the version-problem): Each "modules" shouldn't simply contain the contents of the specific module. It should contain another set of sub-directories which are named like the version tags which they're representing. For example: One folder within there is called "1.0.0", a second one "1.0.1" and another one "1.0.2".

  4. Not the clue about this method: Every module that's getting installed is currently also automatically installing it's own dependencies, right? Then let's keep that like it is! Every time a module gets installed, it's looking into the global module directory and searches for the module which is needed. As soon as it finds the needed module's directory, it takes a look into this directory and looks for the needed version tag. If this version tag exists, it doesn't need to install it again. But if it doesn't exist, the package manager will simply download this version from the repo and put it into it's own folder (which is then named like the version - e.g. "1.0.0"). Summed up: Like this, we're able to keep multiple versions of a single module on our machine, but at the same time, everything is globally and in one place.

  5. Now the only question which is left is: "How can a module/app then require a module?" — Easy: If it depends on the latest version (aka.: higher than "..."), it can simply use require( 'module-name' ). And that, gentlemen, should be the standard. And if the creators of a module simply aren't able to keep up with the updates of their module's dependencies and need to use an older version of a module, they could use something like this:

    require( 'module-1.2.3' )
    

    or this:

    require( 'module' ).v( 1.2.3 )
    

    (I know that those solutions are very dirty, but I'm sure that someone is able to come up with something better as soon as he understands why intention with this post).

diagram

I know that we're not able to change the way how modules work NOW. And that's also not what I want. I just want you to think about how we could shape this whole thing into a more constant, less confusing and less app-specific direction in some future.

Also thank you for taking the time and reading this. If you have an own opinion about that, please drop it into the comment section down below!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions