Package entry points and exports boundaries
Quick answer
A package can expose an intentional public entry point instead of asking consumers to import arbitrary internal files. Node package exports controls supported package-specifier subpaths.
Tooling examples
Example 1. Context: projects/npm-lab. Run in a fresh prepared fixture copy; see its README. Save as example.mjs in that folder; run node example.mjs. Local check available; consult validation evidence for what was asserted.
import { double } from "@lesson/units";console.log(double(4));Expected observation: 8
Example 2. Context: projects/npm-lab. Run in a fresh prepared fixture copy; see its README. Save as example.mjs in that folder; run node example.mjs. Local check available; consult validation evidence for what was asserted.
try { await import("@lesson/units/internal.js"); }catch (error) { console.log(error.code); }Expected observation: ERR_PACKAGE_PATH_NOT_EXPORTED
How the tooling works
The units workspace exposes its root through exports and keeps internal.js unexported. The consumer imports @lesson/units rather than a relative path into its internals. An exports map is a module-resolution interface, not file-system security: direct absolute paths may still reach files. Changes to public exports can break consumers. Keep the implementation behind a small documented API; use separate conditional exports only when needed and test each supported runtime.
Common mistakes
Do not use package internals merely because a file exists in node_modules. Use the supported public entry point.
Practice
Inspect packages/units/package.json, import the public API, then explain the error when the unexported subpath is requested.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Package entry points and exports boundaries — Knowledge Check
Assess tooling contracts, diagnostic reasoning and transfer to a project.
What does package exports primarily define?
Which change risks breaking existing consumers?
Does exports prevent every direct file access?
In Package entry points and exports boundaries, what is the expected observation for example 1 in its documented setup? import { double } from "@lesson/units"; console.log(double(4));
In Package entry points and exports boundaries, what is the expected observation for example 2 in its documented setup? try { await import("@lesson/units/internal.js"); } catch (error) { console.log(error.code); }