Skip to content

esm: introduce `node:interop` to expose the path of the current module

Rodrigo Muino Tomonari requested to merge github/fork/aduh95/node-interop into main

This is an alternative to https://github.com/nodejs/node/pull/48740. This exposes a new "magic" module node:interop that exports the path of the importing module. This aims to simplify uses of paths from within ESM modules, obviously it only works for modules loaded from the file: protocol (i.e. the modules corresponding to files on the FS). I think this would be a better approach than #48740 because it doesn't pollute import.meta, and it provides an early error when it is used from an unsupported protocol, and doesn't go in the way of folks who do not want any path within their ESM code.

Before it can land, this would need:

  • agreement on the principle
  • bikesheding on the module specifier (currently node:interop)
  • bikeshedding on the export names (currently __filename and __dirname)
  • bikeshedding on the error that it should throw when imported from a non-file: protocol (currently ERR_INVALID_URL_SCHEME)
  • Docs

Merge request reports

Loading