自定义的 ESM 说明符解析算法


稳定性: 1 - 实验

不要依赖此标志。我们计划在加载器 API 发展到可以通过自定义加载器实现等效功能时将其删除。

当前的说明符解析不支持 CommonJS 加载器的所有默认行为。 行为差异之一是文件扩展名的自动解析以及导入具有索引文件的目录的能力。

--experimental-specifier-resolution=[mode] 标志可用于自定义扩展解析算法。 默认模式是 explicit,这需要向加载器提供模块的完整路径。 要启用自动扩展解析并从包含索引文件的目录导入,则使用 node 模式。

$ node index.mjs
success!
$ node index # 失败!
Error: Cannot find module
$ node --experimental-specifier-resolution=node index
success!

Stability: 1 - Experimental

Do not rely on this flag. We plan to remove it once the Loaders API has advanced to the point that equivalent functionality can be achieved via custom loaders.

The current specifier resolution does not support all default behavior of the CommonJS loader. One of the behavior differences is automatic resolution of file extensions and the ability to import directories that have an index file.

The --experimental-specifier-resolution=[mode] flag can be used to customize the extension resolution algorithm. The default mode is explicit, which requires the full path to a module be provided to the loader. To enable the automatic extension resolution and importing from directories that include an index file use the node mode.

$ node index.mjs
success!
$ node index # Failure!
Error: Cannot find module
$ node --experimental-specifier-resolution=node index
success!