Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More customizable module resolution #4584

Closed
tblaisot opened this issue Jan 8, 2016 · 2 comments
Closed

More customizable module resolution #4584

tblaisot opened this issue Jan 8, 2016 · 2 comments
Labels
feature request Issues that request new features to be added to Node.js. module Issues and PRs related to the module subsystem.

Comments

@tblaisot
Copy link

tblaisot commented Jan 8, 2016

I am coming from java and maven world and after using npm and node in multi-module project, i think a lot of improvement could be done with module resolution allowing better work with local modules:
Could node have a parameter (let suppose --module_resolution is this command line option ) to be able to provided a file with mapping between module name and path into the filesystem (like the NODE_PATH variable) ?

This file could be a map like

{
    "moduleA" : {
        "path" : "/path/to/moduleA/version/1.0.0",
        "resolve" :  {
            "moduleA" : {
                "path" : "/path/to/moduleB/version/2.0.0",
                "resolve" : { ... an so on ... }
            }
        }
    },
    "moduleB" : {
        "path" : "/path/to/moduleB/version/1.0.0",
        "resolve" : { ... an so on ... }
    }

}

//This map will look a lot like npm-shrinkwrap.json file but with local path instead of url
//I will suppose this file is named module-resolution.json

so running

node /path/to/moduleMain/index.js --module_resolution=/path/to/moduleMain/module-resolution.json

will have this effect:

  • in the launched module (moduleMain/index.js):
    • require("moduleA") will resolve to "/path/to/moduleA/version/1.0.0"
    • require("moduleB") will resolve to "/path/to/moduleB/version/1.0.0"
  • but in moduleA :
    • require("moduleB") will resolve to "/path/to/moduleB/version/2.0.0"

Avantages:

  • allow the same flexibility than nested node_modules folders (multiple version of the same module name, each module being able to import the choosen version)
  • optimize module resolution because there no more need to iterate going up the filesystem tree to find a node-module folder containing the module
  • all modules and version can be installed in one local repository (like npm-cache but with exploded tgz)
  • no need for linking global modules into local node_module folder
  • no need to fetch and install the same dependencies multiple time for multiple modules with the same dependency
  • no more need for linking folders for local dev with multiple modules
  • no need for npm-dedupe

This file could be generated by modules managers like npm or bower.
Differents modules managers can handle multiple versions differently easily (if a module manager want to only allow one version of a module to be used, it just have to modify this map)

This file could be agnostic of the resolution of the js file from the module name so externals tools could use this file for require resolution:
algorithme like
looking for main field in /resolved/path/from/the/map/package.json for node or webpack
or
looking for typing field in /resolved/path/from/the/map/package.json for typescript compiler
can be let to the executor discretion.

@mscdex mscdex added module Issues and PRs related to the module subsystem. feature request Issues that request new features to be added to Node.js. labels Jan 8, 2016
@sindresorhus
Copy link

The module resolution is locked.

@cjihrig
Copy link
Contributor

cjihrig commented Jan 8, 2016

Your issue, nodejs/NG#27, is the better place for this discussion. Closing in favor of that.

@cjihrig cjihrig closed this as completed Jan 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. module Issues and PRs related to the module subsystem.
Projects
None yet
Development

No branches or pull requests

4 participants