Skip to content

Proposal: require(<module>[, <version>]) + npm: put version in module install path #4744

Closed
@blitmap

Description

@blitmap

Recently npm changed to install dependencies in a 'flatter' way.

https://docs.npmjs.com/how-npm-works/npm3

This is just my naive proposal but I'd like to suggest 2 things:

  1. Add a 2nd parameter to require(<module>, <version>) for requesting a specific semantic version
  • require('./module') would be equivalent to: require('./module', '*')
    • I've never used '' in package.json, does it take the latest version or the first it finds?
  1. Install modules with their version in the path: node_modules/moment/1.4.8/index.js

Note: Modules sometimes change names, so you'd see node_modules/extendr/1.4.2 and then node_modules/extendible/1.4.3. A later version with a different module name would not go to the same node_modules/extendr.

I believe this would make it possible to install all dependencies in the first level of node_modules and allow loading modules like slots.

My reasoning is that if you need a specific version of something it should be reflected in your source code, not just package.json.

This would be making a change to require() which is 'Locked', but because it's an optional 2nd parameter I think it's acceptable to break. Previously I had thought require('./module', 'a', 'b', 'c') might pass those extra arguments to a module in arguments - but it seems not. Providing a version there would be a non-breaking change.

Side-concern: npm ls --depth=0 would show all dependencies - not just your 'primary' dependencies. Some scripts might rely on this.


PS: I realize the 2 is npm-related, not node-related. I'm proposing this with the full text in both projects for completeness/coherency.

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