自定义 ESM 说明符解析算法


【Customizing ESM specifier resolution algorithm】

稳定性: 1 - 实验性

不要依赖此标志。我们计划在 模块自定义钩子 发展到可以通过自定义钩子实现同等功能时移除它。

当前的指定符解析不支持 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!