Home › npm & Node.js

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath is not defined by "exports" (Node.js) — how to fix

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/utils' is not defined by "exports" in /app/node_modules/some-pkg/package.json
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /app/node_modules/some-pkg/package.json

Quick fix: Import only the entry points the package exports (check exports in node_modules//package.json), e.g. import { v4 } from 'uuid' instead of require('uuid/v4'). If the error is No "exports" main defined, the package is probably ESM-only: use import instead of require(). If it comes from a build tool, upgrade that tool.

What it means

A package can restrict which files consumers can load with an exports field:

{
  "name": "some-pkg",
  "exports": {
    ".": { "import": "./dist/index.mjs", "require": "./dist/index.cjs" },
    "./utils": "./dist/utils.js"
  }
}

With this, only some-pkg and some-pkg/utils can be imported. Anything else, like some-pkg/lib/utils or some-pkg/package.json, throws ERR_PACKAGE_PATH_NOT_EXPORTED, even though the file exists on disk.

Look at the real map to see what's allowed:

cat node_modules/some-pkg/package.json

Cause 1: a deep import into package internals

Code (often copied from an old tutorial) imports an internal file. When the package added exports in a new major version, those paths stopped working.

Fix 1

Use a public entry point. A classic example is uuid, which removed deep requires in version 8:

// old
const uuidv4 = require('uuid/v4');

// current
const { v4: uuidv4 } = require('uuid');
// or
import { v4 as uuidv4 } from 'uuid';

Check the package's README or changelog for the replacement. If you really need the old path, pin the previous major version in package.json until you can migrate.

Cause 2: No "exports" main defined with require()

The exports map only has an import condition, so the package is ESM-only and has no entry for require():

"exports": { ".": { "import": "./index.js" } }

Fix 2

Load it as an ES module. In an ESM file (.mjs or "type": "module"):

import pkg from 'some-pkg';

From CommonJS, use dynamic import:

const { default: pkg } = await import('some-pkg');

Or install the last version that still shipped CommonJS. Many popular packages (node-fetch 3, chalk 5, nanoid 4 and others) went ESM-only in a major release, and their previous major still supports require().

Cause 3: an old build tool on a newer package or Node version

The error points inside node_modules of a bundler, loader or framework (e.g. an old webpack loader or react-scripts setup), which deep-imports a dependency that has since added exports.

Fix 3

Upgrade the tool that does the import, not the package it imports:

npm ls some-pkg          # see who depends on it
npm install tool-name@latest

If the tool can't be upgraded, an overrides entry in your package.json can pin the dependency to an older version that has no exports restriction:

{ "overrides": { "some-pkg": "1.2.3" } }

Run npm install afterwards.

Cause 4: TypeScript resolves differently from Node

With "moduleResolution": "node" (node10), TypeScript ignores exports, so code that type-checks fails at runtime; with NodeNext or Bundler, TypeScript enforces the map at compile time too.

Fix 4

Set "moduleResolution": "NodeNext" (Node projects) or "Bundler" (Vite/webpack projects) in tsconfig.json so the compiler reports invalid subpaths before you run the code.

Last updated 2026-11-30.