liveBook · Manning

Exports field

As explained in Secrets of the JavaScript Ninja, Third Edition

The exports field is a package.json field that defines which package subpaths and entry points can be imported.

The exports field in package.json maps package import specifiers to files. It can define an entry point for the package root and expose selected subpaths; it is the modern alternative to the older main field. section 13.1.2

A mapping can use:

  • "." for imports using only the package name, and a key such as "./ninjas/*" for matching subpaths. section 13.1.2
  • Relative file paths as values; wildcard matches in a key map one-to-one to wildcards in its value. section 13.1.2
Listing 13.9 Defining entry points for a package
{
  "name": "entry-point-example",
  "private": true,
  "type": "module", #A
  "exports": { #B
    ".": "./lib/index.js", #B
    "./ninjas/*": "./lib/ninjas/*.js" #B
  } #B
}

This configuration maps the package root to its index file and ninja subpaths to matching files. section 13.1.2

After Node.js finds a package, it checks its package.json for an exports mapping. When debugging resolution, inspect the manifest in the local node_modules package: it may differ from the package’s source manifest. section 3.2.2 section 13.1.2

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Maps package roots and wildcard subpaths, while leaving broader exports options unspecified.
Examples
Uses a package-root export and wildcard ninja subpath to import turtle functions.
What it assumes
Assumes package-import syntax and package.json; traces entry-point lookup after node_modules resolution.
What you can do after
Configure package entry points by mapping root and subpath imports to relative files.
How it connects
Connects package.json manifests, package entry points, bare specifiers, and Node.js module resolution.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.