Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Node.js ERR_PACKAGE_PATH_NOT_EXPORTED: subpath fix

ERR_PACKAGE_PATH_NOT_EXPORTED means the package exports map never declared the path you imported. Use a declared subpath, the root entry, or fix the map.

~5 min readNode.js
#Node.js#ESM#Packages#Errors
A Node package crate with a locked cyan exports gate, an amber subpath card rejected outside it

Introduction & Quick Summary

Importing a path a package never published fails before any of your code runs:

code
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/utils' is not defined by "exports" in /app/node_modules/some-pkg/package.json imported from /app/src/index.js

The message is literal. some-pkg/package.json contains an exports field, and that field does not list ./lib/utils. Node.js no longer falls back to the file path when exports exists, so a deep import that worked before the package adopted exports now fails. The same happens with require when the map only exposes an import condition, and with TypeScript when the map does not expose types.

The three honest fixes:

  • Use a published subpath. Read the map and import a path it declares, such as some-pkg/utils.
  • Import the root. Most packages re-export the useful surface from the main entry.
  • Publish the subpath if the package is yours, by adding the path to exports with the right conditions.

Editing node_modules, deep-requiring with a relative path into the package directory, or monkey-patching the resolver all work until the next install, which is why they are not fixes.

Core Mechanics, Signatures & Reproducible Setup

When package.json has an exports field, it becomes the complete list of entry points. main is ignored for resolution in that case, and any subpath not matched by a key is rejected. Keys are either exact ("./utils") or patterns ("./features/*"), and each value is a set of conditions Node.js evaluates in the order they appear.

{ "name": "some-pkg", "type": "module", "exports": { ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js", "require": "./dist/index.cjs" }, "./utils": { "types": "./dist/utils.d.ts", "import": "./dist/utils.js" }, "./features/*": "./dist/features/*.js" } }

Reproduce with a small script so the resolution failure is isolated from the framework you are debugging:

bash
node -e "console.log(require.resolve('some-pkg/lib/utils'))" # CJS node --input-type=module -e "import('some-pkg/lib/utils').catch(e => console.log(e.code, e.message))"

The same package can produce three different messages, and each points at a different part of the map:

MessageMeaningFirst check
Package subpath './x' is not defined by "exports"key absent from the mapthe exports keys
No "exports" main defined in ...no "." entrythe map's root key
No known conditions for "./x" specifierkey exists, condition does notimport versus require
Cannot find moduleno exports, wrong path on diskinstall state and main

Key Comparison Table

FixWorks next installEffortCorrect when
Import a declared subpathyesminutesthe package exports an equivalent
Import the root entryyesminutesthe root re-exports the symbol
Add the subpath to exportsyesone commityou maintain the package
patch-package the dependencyuntil the version changeshightemporary unblock with a fork planned
Relative path into node_modulesnolownever in application code
Bundler alias to the filefragilemediumbuild-time only, documented

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Read the exports map before guessing

Print the field instead of searching documentation that may describe an older version.

bash
node -e "const p=require('./node_modules/some-pkg/package.json'); console.log(JSON.stringify(p.exports,null,2))" # or, from a script inside the project node -e "console.log(require('some-pkg/package.json').exports)"

If the second command itself fails with ERR_PACKAGE_PATH_NOT_EXPORTED, the map does not expose ./package.json. Read the file directly from node_modules in that case, or check the published files list.

2. require against an import-only package

A CommonJS file requiring a package that only defines import gets the same family of error, because the require condition is absent.

js
// index.cjs const pkg = require("esm-only-pkg"); // no "require" condition in exports

Two legitimate options: load it dynamically from CJS with await import("esm-only-pkg"), or keep the consumer ESM ("type": "module"). Adding a bundler that resolves the import condition also works, but only if the code is actually bundled for a runtime that supports ESM.

3. TypeScript cannot see the types for a declared subpath

moduleResolution: "bundler" or "node16" reads the exports map for types too. A subpath without a types condition resolves at runtime and fails type-checking with TS2307.

{ "compilerOptions": { "module": "nodenext", "moduleResolution": "nodenext" } }

With these settings the compiler follows the same rules as Node, so an error appears at the import rather than at runtime. If the package ships no types at all, install @types/... or declare a module shim with the exact subpath.

In a monorepo, some-pkg may resolve to a workspace symlink whose package.json is the source, not the built output. The exports map then points at dist files that do not exist until the package is built.

bash
ls -l node_modules/some-pkg # symlink into packages/ npm ls some-pkg # duplicated copies?

If the map points at build output, build the workspace package before the app, or add a development condition such as "development": "./src/index.ts" that your tooling selects.

5. Wildcard subpaths

A pattern key covers every path under a prefix, which is why one package accepts some-pkg/features/a and another rejects it.

{ "exports": { "./features/*": "./dist/features/*.js" } }

The * captures the rest of the specifier, including slashes. A package that only declares "./features/*.js" expects callers to include the extension, which surprises everyone; check the exact pattern before changing the import.

6. You maintain the package

If the missing subpath should be public, add it to the map rather than telling consumers to deep-import. Publish types alongside the runtime file, and keep conditions ordered so types comes before import and require.

{ "exports": { ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js", "require": "./dist/index.cjs" }, "./utils": { "types": "./dist/utils.d.ts", "import": "./dist/utils.js", "require": "./dist/utils.cjs" } } }

Also add the file to files in package.json. A map entry pointing at an unshipped file produces Cannot find module after install, which is a slower failure to diagnose.

Common Compilation/Runtime Mistakes & Version Gotchas

  • Assuming main still wins. Once exports exists, main is only relevant to legacy resolvers. Many older tools still read it, so inconsistent behaviour between the bundler and Node is expected.
  • Testing with a bundler only. Vite and webpack are more permissive than Node; a build that succeeds can still crash in a plain Node script or a serverless runtime.
  • Jest resolution. Jest uses its own resolver and older versions ignored exports. Upgrading Jest changes which paths resolve and can surface errors that were always there.
  • Copying files into node_modules. Any install rewrites the directory. Use a patch file if the change must survive, and open the upstream issue.
  • Condition order. Conditions are matched in the order written. Putting require before import is valid but changes which file a dual-mode consumer receives.
SymptomCauseFix
works in webpack, fails in Noderesolver laxityuse a declared subpath
fails after dependency upgradeexports addedread the new map
fails only in testsJest resolveralign Jest config or upgrade
fails only in production imagemissing built distbuild the workspace package

Package metadata is easier to read as formatted JSON, so paste the package.json into the JSON formatter when comparing two versions, and use the text diff tool to see exactly which export key a lockfile bump removed.

The rules are specified in the Node.js documentation for package entry points and the error itself is listed under Node.js errors. Both describe conditions and patterns more precisely than any summary, and they are short.

Official references

primary documentation

The Node.js documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Node.js work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Node.js guides

More in Node.js

All Node.js guides

The guides filed next to this one in Node.js, in library order.