package.json exports resolver (which file Node loads)

v1.0.0
io.github.Lawatch/package-exports Checked 3 days ago Published 19 days ago

Which file Node loads for import or require of pkg/sub, and why: package.json exports, same as Node.

These tools were reported by the server itself during our smoke test (tools/list), not copied from the README.

resolve_package_import Which file does Node load for this import?

Answers "which file does `import 'pkg/sub'` or `require('pkg/sub')` load, and why?" exactly like Node 22: package.json "exports" (subpaths, * patterns, nested conditions, arrays, null), "imports" (#internal specifiers) and the old "main" fallback. Gives the file, its format (ES module or CommonJS), the key and the chain of conditions that decided (e.g. "./client" › import › default), a step-by-step trace, and Node's exact error code when it fails (ERR_PACKAGE_PATH_NOT_EXPORTED, ERR_UNSUPPORTED_DIR_IMPORT, ERR_PACKAGE_IMPORT_NOT_DEFINED, MODULE_NOT_FOUND…). By default it answers for both…

ParameterTypeDescription
specifier *stringWhat the code imports: "react-dom/client", "@scope/pkg/utils", "#internal/x", "." for the package itself, or "./sub" (a subpath of the given package).
conditionsarrayExtra conditions, like `node --conditions` / `-C` (e.g. ["development"], ["browser"], ["react-server"]). Node always uses "default", plus "node", "import" or "require", "module-sync" and "node-addons"…
exact_conditionsarrayOr: the full condition list, replacing Node's defaults (to mimic a bundler, e.g. ["browser", "import", "module"]). "default" always matches.
filesarrayOptional with package_json: the files of the package (e.g. "dist/index.mjs"), so missing files and folders are reported as Node would. Without it, existence is not checked.
modestringimport (ES modules, import / import()), require (CommonJS), or both (default).
package_dirstringFolder of the package (the one with its package.json), or a project folder: then the package named in "specifier" is looked up in node_modules, like Node does. Files are checked for real. Easiest when…
package_jsonobject | stringOr: the content of the package.json (object or JSON text).

list_package_exports What does this package expose, and what is wrong with its exports?

Lists every public entry point of a package from package.json "exports" (and "imports"), with the file that import and require each load, the files each * pattern really exposes (with package_dir), and the mistakes Node will not tell you about: conditions written after "default" (never used), "types" not first (TypeScript misses the types), targets without "./", folder keys ending in "/" (removed in Node 17), entries pointing to missing files, "main" ignored because "exports" has no ".", import and require loading different files (dual package hazard), "./package.json" not exported. Use it bef…

ParameterTypeDescription
conditionsarrayExtra conditions, like `node --conditions` / `-C` (e.g. ["development"], ["browser"], ["react-server"]). Node always uses "default", plus "node", "import" or "require", "module-sync" and "node-addons"…
exact_conditionsarrayOr: the full condition list, replacing Node's defaults (to mimic a bundler, e.g. ["browser", "import", "module"]). "default" always matches.
filesarrayOptional with package_json: the files of the package (e.g. "dist/index.mjs"), so missing files and folders are reported as Node would. Without it, existence is not checked.
modestringDefault both.
packagestringWith a project folder in package_dir: the package to inspect in node_modules (e.g. "react-dom").
package_dirstringFolder of the package (the one with its package.json), or a project folder: then the package named in "specifier" is looked up in node_modules, like Node does. Files are checked for real. Easiest when…
package_jsonobject | stringOr: the content of the package.json (object or JSON text).