自定义 ESM 说明符解析算法


¥Customizing ESM specifier resolution algorithm

稳定性: 1 - 实验性的

¥Stability: 1 - Experimental

不要依赖这个标志。我们计划在 加载器 API 改进到可以通过自定义加载程序实现等效功能的程度后将其删除。

¥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.

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

¥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.

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

¥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!