resolve-pkg-maps

中文文档
1.0.0发布于 2022/12/14MITTypeScriptESM + CJS零依赖近 30 天下载 4亿

Resolve package.json exports & imports maps

原始使用文档
文档目录10 节
  1. Usage
  2. Resolving exports
  3. Resolving imports
  4. API
  5. resolveExports(exports, request, conditions)
  6. resolveImports(imports, request, conditions)
  7. Errors
  8. FAQ
  9. Why do the APIs return an array of paths?
  10. How is it different from resolve.exports ?

resolve-pkg-maps

Utils to resolve package.json subpath & conditional exports/imports in resolvers.

Implements the ESM resolution algorithm. Tested against Node.js for accuracy.

Support this project by ⭐️ starring and sharing it. Follow me to see what other cool projects I'm working on! ❤️

Usage

Resolving exports

utils/package.json

{
    // ...
    "exports": {
        "./reverse": {
            "require": "./file.cjs",
            "default": "./file.mjs"
        }
    },
    // ...
}
import { resolveExports } from 'resolve-pkg-maps'

const [packageName, packageSubpath] = parseRequest('utils/reverse')

const resolvedPaths: string[] = resolveExports(
    getPackageJson(packageName).exports,
    packageSubpath,
    ['import', ...otherConditions]
)
// => ['./file.mjs']

Resolving imports

package.json

{
    // ...
    "imports": {
        "#supports-color": {
            "node": "./index.js",
            "default": "./browser.js"
        }
    },
    // ...
}
import { resolveImports } from 'resolve-pkg-maps'

const resolvedPaths: string[] = resolveImports(
    getPackageJson('.').imports,
    '#supports-color',
    ['node', ...otherConditions]
)
// => ['./index.js']

API

resolveExports(exports, request, conditions)

Returns: string[]

Resolves the request based on exports and conditions. Returns an array of paths (e.g. in case a fallback array is matched).

exports

Type:

type Exports = PathOrMap | readonly PathOrMap[]

type PathOrMap = string | PathConditionsMap

type PathConditionsMap = {
    [condition: string]: PathConditions | null
}

The exports property value in package.json.

request

Type: string

The package subpath to resolve. Assumes a normalized path is passed in (eg. repeating slashes //).

It should not start with / or ./.

Example: if the full import path is some-package/subpath/file, the request is subpath/file.

conditions

Type: readonly string[]

An array of conditions to use when resolving the request. For reference, Node.js's default conditions are ['node', 'import'].

The order of this array does not matter; the order of condition keys in the export map is what matters instead.

Not all conditions in the array need to be met to resolve the request. It just needs enough to resolve to a path.


resolveImports(imports, request, conditions)

Returns: string[]

Resolves the request based on imports and conditions. Returns an array of paths (e.g. in case a fallback array is matched).

imports

Type:

type Imports = {
    [condition: string]: PathOrMap | readonly PathOrMap[] | null
}

type PathOrMap = string | Imports

The imports property value in package.json.

request

Type: string

The request resolve. Assumes a normalized path is passed in (eg. repeating slashes //).

Note: In Node.js, imports resolutions are limited to requests prefixed with #. However, this package does not enforce that requirement in case you want to add custom support for non-prefixed entries.

conditions

Type: readonly string[]

An array of conditions to use when resolving the request. For reference, Node.js's default conditions are ['node', 'import'].

The order of this array does not matter; the order of condition keys in the import map is what matters instead.

Not all conditions in the array need to be met to resolve the request. It just needs enough to resolve to a path.


Errors

ERR_PACKAGE_PATH_NOT_EXPORTED

  • If the request is not exported by the export map

ERR_PACKAGE_IMPORT_NOT_DEFINED

  • If the request is not defined by the import map

ERR_INVALID_PACKAGE_CONFIG

  • If an object contains properties that are both paths and conditions (e.g. start with and without .)
  • If an object contains numeric properties

ERR_INVALID_PACKAGE_TARGET

  • If a resolved exports path is not a valid path (e.g. not relative or has protocol)
  • If a resolved path includes .. or node_modules
  • If a resolved path is a type that cannot be parsed

FAQ

Why do the APIs return an array of paths?

exports/imports supports passing in a fallback array to provide fallback paths if the previous one is invalid:

{
    "exports": {
        "./feature": [
            "./file.js",
            "./fallback.js"
        ]
    }
}

Node.js's implementation picks the first valid path (without attempting to resolve it) and throws an error if it can't be resolved. Node.js's fallback array is designed for forward compatibility with features (e.g. protocols) that can be immediately/inexpensively validated:

{
    "exports": {
        "./core-polyfill": ["std:core-module", "./core-polyfill.js"]
    }
}

However, Webpack and TypeScript have deviated from this behavior and attempts to resolve the next path if a path cannot be resolved.

By returning an array of matched paths instead of just the first one, the user can decide which behavior to adopt.

How is it different from resolve.exports?

resolve.exports only resolves exports, whereas this package resolves both exports & imports. This comparison will only cover resolving exports.

  • Despite it's name, resolve.exports handles more than just exports. It takes in the entire package.json object to handle resolving . and self-references. This package only accepts exports/imports maps from package.json and is scoped to only resolving what's defined in the maps.

  • resolve.exports accepts the full request (e.g. foo/bar), whereas this package only accepts the requested subpath (e.g. bar).

  • resolve.exports only returns the first result in a fallback array. This package returns an array of results for the user to decide how to handle it.

  • resolve.exports supports subpath folder mapping (deprecated in Node.js v16 & removed in v17) but seems to have a bug. This package does not support subpath folder mapping because Node.js has removed it in favor of using subpath patterns.

  • Neither resolvers rely on a file-system

This package also addresses many of the bugs in resolve.exports, demonstrated in this test.

版本与发布记录

resolve-pkg-maps 共发布 1 个版本,首次发布于 2022/12/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2022/12/143 年前

关于 resolve-pkg-maps 的常见问题

resolve-pkg-maps 是什么?

Resolve package.json exports & imports maps

如何安装 resolve-pkg-maps?

在项目目录运行 npm install resolve-pkg-maps,或使用 pnpm add / yarn add / bun add。

resolve-pkg-maps 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

resolve-pkg-maps 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

resolve-pkg-maps 可以商用吗?

resolve-pkg-maps 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

resolve-pkg-maps 的下载量有多少?

根据 npm 官方下载统计,resolve-pkg-maps 在 2026/10/05 – 2026/11/03 这 30 天内被下载 398,894,423 次,日均约 13,296,481 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

resolve-pkg-maps 还在维护吗?

最新版本 1.0.0 发布于 2022/12/14(3 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/12/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。