package.json fields and exports

Intermediate · 15 min

What name, version, type, main, exports, imports, engines, files and bin do in package.json, and how conditional and subpath exports control your public API.

Before thisHow Node resolves modules

A shot of code · 15 min

package.json fields and exports

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by A shot of codeReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `package.json` is both metadata for npm (`name`, `version`, `files`, `engines`, `license`) and instructions for Node (`type`, `main`, `exports`, `imports`, `bin`). Knowing which field is read by whom stops a lot of guessing.
  • `main` names one entry file. `exports` replaces it with a map that can expose several entry points (`"."`, `"./utils"`), choose different files for `import` and `require`, and forbid every path not listed.
  • Conditions inside `exports` are tried in object order and the first match wins: put specific ones such as `types`, `import` and `require` before `default`.
  • `imports` defines private aliases that start with `#` and are only visible inside your own package. They are a standards-based replacement for relative-path ladders such as `../../../utils`.
  • `files` is an allow-list for what `npm publish` packs. Without it you ship everything not in `.npmignore` or `.gitignore`; with it, you ship `package.json`, README, LICENSE and the listed paths.

One file, several readers

package.json is read by different programs for different fields. npm reads name, version, dependencies, scripts, files, engines and license. Node itself reads type, main, exports, imports and, when you run node --run, scripts. Bundlers and TypeScript read module, types, sideEffects and their own additions. A lot of “why isn’t my package working” is a field being set for the wrong reader.

The fields you set on every project

{
  "name": "@acme/tiny",
  "version": "1.2.0",
  "description": "A tiny example",
  "license": "MIT",
  "type": "module",
  "engines": { "node": ">=24" },
  "files": ["dist"],
  "scripts": { "test": "node --test" }
}
  • name and version identify a package. Together they must be unique on the registry, and the name has rules (lowercase, no spaces, scopes like @acme/ allowed). Both are required only if you publish.
  • type picks the module system for .js files, covered in ES modules in Node and interop.
  • engines states which Node versions work; npm warns by default (installing Node and managing versions).
  • files is what gets published. See below.
  • private: true makes npm refuse to publish, which is a cheap safety net for applications and monorepo roots.

main versus exports

main is the old way to say “this file is the package’s entry”. It has two limits: one entry point only, and no access control, because every file in the package is reachable by deep path.

exports fixes both. In its simplest form it is a string:

{ "exports": "./dist/index.js" }

That makes import 'pkg' work and every other path private. With an object, you expose subpaths:

{
  "exports": {
    ".": "./dist/index.js",
    "./utils": "./dist/utils.js",
    "./package.json": "./package.json"
  }
}

Testing a package like this on Node 26.13.1: import 'lib' and import 'lib/utils' work, while import 'lib/internal/secret.js' and even import 'lib/dist/utils.js' both throw ERR_PACKAGE_PATH_NOT_EXPORTED. The second one surprises people. The file exists, but it was reachable through ./utils, not by its real path. That is the encapsulation: you can rename or move internals without breaking users, because users could only reach what you listed. The cost is that anything you list is a public promise. Expose ./package.json explicitly if tools need to read it.

Pattern exports expose a whole folder: "./features/*": "./dist/features/*.js".

Conditional exports

An export target can be an object of conditions. Node picks the first condition that matches how the package is being loaded:

{
  "exports": {
    ".": {
      "import": "./dist/index.js",
      "require": "./dist/index.cjs",
      "default": "./dist/index.js"
    }
  }
}

With this, import in an ES module gets index.js and require gets index.cjs, which is how a package ships both formats. In a test, import entry from 'lib' returned the ESM file and require('lib') returned the CJS file from the same package. The order rules matter:

  • Conditions are tried in the order written, first match wins.
  • default always matches, so it goes last.
  • Common conditions are import, require, node, browser, types (TypeScript, placed first), development and production (used by some tools).

Since Node 22.12 you can require() an ES module, so a package that is purely ESM no longer needs a CommonJS twin for Node users. If you still support older runtimes or bundlers, keep the require condition. Publishing types for TypeScript consumers is covered in publishing a typed library.

imports: private aliases

exports is for outsiders. imports is for you. Keys must start with #:

{
  "imports": {
    "#db": { "node": "./src/db.js", "default": "./src/db-browser.js" },
    "#utils/*": "./src/utils/*.js"
  }
}

Then import db from '#db' and import { slug } from '#utils/text' work from anywhere in the package, instead of ../../../utils/text.js. In a test on Node 26.13.1 both resolved correctly. Because they are standard, tools such as TypeScript and bundlers understand them without extra plugins, unlike older path-alias setups.

files and bin

files is an allow-list for npm publish. A test package with "files": ["dist"] plus a src/ and test/ folder packed only LICENSE, README.md, dist/index.js and package.json: npm always includes the README, LICENSE and package.json. Always check with npm pack --dry-run before publishing; publishing an npm package uses it.

bin declares executable commands that npm links onto the user’s PATH, used in building command-line tools.

What the video covers

“A shot of code” gives a whole 15-minute video to the exports keyword: why main is not enough, and how exports exposes some functionality while encapsulating the rest. It is from August 2021 and a small channel. exports semantics have been stable since Node 12 to 16, so it holds up, but its title and description say nothing of conditional exports, imports or files, so those sections are the prose’s. Codevolution’s package.json tour under “go deeper” covers the basic fields for beginners. No transcript was available.

Where this fits

This is the author’s side of resolution. Next, npm scripts and lifecycle hooks covers the scripts field and what runs when. To read how npm and the lockfile behave in general, see package managers and npm.

Check yourself

  1. 01A package has `"exports": { ".": "./dist/index.js" }`. A user runs `import x from 'pkg/dist/helpers.js'`. What happens, and what should the author do if that path should be supported?

    Node throws `ERR_PACKAGE_PATH_NOT_EXPORTED`, because `exports` blocks every path it does not list. To support it, add an entry such as `"./helpers": "./dist/helpers.js"` (or a pattern `"./*": "./dist/*.js"`) and treat it as public API from then on.

  2. 02Why put the `default` condition last in an `exports` entry?

    Conditions are checked in the order they are written and the first one that matches is used. `default` always matches, so anything listed after it is never reached.

  3. 03What is the difference between `main` and `exports`, and which wins?

    `main` is a single entry file for the whole package, with no way to restrict deep imports. `exports` is a map of entry points that can vary by condition and blocks unlisted paths. When both exist, current Node uses `exports`, and `main` is a fallback for old tools.

  4. 04You publish a package and users report that your `src/` and test files are in the tarball. How do you control that?

    Add a `files` array to `package.json` listing what to ship, for example `["dist"]`. `npm pack --dry-run` lists exactly what would be published, so you can check before publishing.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark