目标必须是相对 URL


¥Targets must be relative URLs

"exports" 映射中的所有目标路径(与导出键关联的值)必须是以 ./ 开头的相对 URL 字符串。

¥All target paths in the "exports" map (the values associated with export keys) must be relative URL strings starting with ./.

// package.json
{
  "name": "my-package",
  "exports": {
    ".": "./dist/main.js",          // Correct
    "./feature": "./lib/feature.js", // Correct
    // "./origin-relative": "/dist/main.js", // Incorrect: Must start with ./
    // "./absolute": "file:///dev/null", // Incorrect: Must start with ./
    // "./outside": "../common/util.js" // Incorrect: Must start with ./
  }
} 

此行为的原因包括:

¥Reasons for this behavior include:

  • 安全性:防止从包自身目录之外导出任意文件。

    ¥Security: Prevents exporting arbitrary files from outside the package's own directory.

  • 封装:确保所有导出的路径都相对于包根目录进行解析,从而使包自包含。

    ¥Encapsulation: Ensures all exported paths are resolved relative to the package root, making the package self-contained.