Expose file resolution as a utility #51855
Labels
esm
Issues and PRs related to the ECMAScript Modules implementation.
feature request
Issues that request new features to be added to Node.js.
loaders
Issues and PRs related to ES module loaders
What is the problem this feature will solve?
Tool authors often need to reimplement Node's resolving behaviors in order to ask the question: if this file was to import this path, which file would Node resolve that to?
This comes up for build tools like Vite, Esbuild, or Webpack. It also comes up for analysis tools like Eslint and Typescript.
For a long time, people were able to get by with the likes of https://www.npmjs.com/package/resolve. But the introduction of the full suite of package.json
exports
features (conditions, subpath imports, etc) and ESM have resulted in a situation where almost no tool actually covers Node's complete spec correctly anymore. It seems wasteful to try to fix all these parallel implementations rather than share node's implementation as a utility.What is the feature you are proposing to solve the problem?
A node-provided, importable constructor that takes conditions:
Where the
Resolver
instance offers a method with the signature:The key features provided here that are not provided (AFAIK) by any existing Node API are:
--conditions
that prevail in the current node process (remember -- we're talking about running this inside a tool like a linter. Regardless of what conditions the linter uses for its own implementation, it should follow imports the way the user's program is going to do it, using the user's conditions.)require.resolve
which, even if you setpaths
to the inside a package doesn't respect the self-referencing rule.Note that I'm definitely not asking for access to the state of the current node process's resolver. I don't want its customized loaders, I don't want to share its cache. Rather, this would be asking to set up an entirely separate state that would not interfere with Node's own module loading at all.
What alternatives have you considered?
There are lots of third-party implementations, maintained at great effort and all with incomplete support for Node's behaviors. In theory somebody could invest the effort to make one of them perfect and continually invest in making it follow new node features.
The text was updated successfully, but these errors were encountered: