Modules

Package exports and imports

The `exports` field declares which package entry points consumers may import by package name. The `imports` field provides internal aliases beginning with `#` for code inside the package. Set `type` explicitly for .js files; .mjs and .cjs retain their module modes. Map `.` to the main public entry, and `./feature` to an intended subpath. When exports is present, unlisted subpaths are not available through package-name resolution; this is API encapsulation, not a security boundary. Conditional exports select the first matching condition in object order. Keep explicit file extensions in relative ESM imports. The fixtures create a tiny package in a disposable working directory, then run its self-reference or internal alias; no dependency install occurs. An exports map does not make files confidential. Do not package private material or treat unexported subpaths as a permission system. Sources: https://nodejs.org/download/release/v22.16.0/docs/api/packages.html.

Formal Syntax & Structure

nodejs syntaxnodejs
package.json: {"exports": {".": "./public.js"}, "imports": {"#price": "./price.js"}}

Working Example

Example Snippetnodejs
import {writeFileSync} from 'node:fs';import assert from 'node:assert/strict';writeFileSync('package.json',JSON.stringify({name:'bm-fixture',type:'module',exports:{'.':'./public.js'}}));writeFileSync('public.js','export const count=3;');writeFileSync('private.js','export const internal=7;');assert.equal((await import('bm-fixture')).count,3);await assert.rejects(import('bm-fixture/private.js'),{code:'ERR_PACKAGE_PATH_NOT_EXPORTED'});console.log('public allowed; private subpath denied');
Node.js Reference