NPM.Resolution.Exports (NPM v0.6.0)

Copy Markdown View Source

Parse and resolve the exports field from package.json.

Modern npm packages use the exports field (a.k.a. "export map") to define entry points and restrict access to internal modules.

Supports:

  • String shorthand: "exports": "./index.js"
  • Subpath exports: "exports": { ".": "./index.js", "./utils": "./lib/utils.js" }
  • Conditional exports: "exports": { "import": "./esm.js", "require": "./cjs.js" }
  • Nested conditions: "exports": { ".": { "import": "./esm.js", "default": "./cjs.js" } }

Summary

Functions

Extracts all conditions used in the export map.

Checks if a subpath is exported by the export map.

Detect whether a package uses ESM (type: "module") or CJS.

Parse the exports field from a package.json map.

Resolve an import path against an export map.

List all exported subpaths from an export map.

Validates that all export paths resolve to existing files.

Types

export_map()

@type export_map() ::
  String.t() | [export_map()] | %{required(String.t()) => export_map()} | nil

Functions

conditions(export_map)

@spec conditions(map() | nil) :: [String.t()]

Extracts all conditions used in the export map.

exported?(subpath, export_map)

@spec exported?(String.t(), map() | nil) :: boolean()

Checks if a subpath is exported by the export map.

module_type(arg1)

@spec module_type(map()) :: :esm | :cjs

Detect whether a package uses ESM (type: "module") or CJS.

parse(arg1)

@spec parse(map()) :: %{required(String.t()) => String.t() | map()} | nil

Parse the exports field from a package.json map.

Returns a normalized map of subpath → target mappings, or nil if no exports field.

resolve(export_map, subpath, conditions \\ ["default"])

@spec resolve(map(), String.t(), [String.t()]) :: {:ok, String.t()} | :error

Resolve an import path against an export map.

Given a subpath (e.g. ".", "./utils") and a list of conditions (e.g. ["import", "default"]), returns the resolved file path.

subpaths(export_map)

@spec subpaths(map()) :: [String.t()]

List all exported subpaths from an export map.

validate(export_map, base_dir)

@spec validate(map() | nil, String.t()) ::
  {:ok, [String.t()]} | {:error, [String.t()]}

Validates that all export paths resolve to existing files.