子路径导出


【Subpath exports】

在使用 "exports" 字段时,可以定义自定义子路径以及主入口点,通过将主入口点视为 "." 子路径来实现:

【When using the "exports" field, custom subpaths can be defined along with the main entry point by treating the main entry point as the "." subpath:】

{
  "exports": {
    ".": "./index.js",
    "./submodule.js": "./src/submodule.js"
  }
} 

现在只有在 "exports" 中定义的子路径可以被使用者导入:

【Now only the defined subpath in "exports" can be imported by a consumer:】

import submodule from 'es-module-package/submodule.js';
// Loads ./node_modules/es-module-package/src/submodule.js 

而其他子路径会出错:

【While other subpaths will error:】

import submodule from 'es-module-package/private-module.js';
// Throws ERR_PACKAGE_PATH_NOT_EXPORTED