Skip to content

Resolving ERR_PACKAGE_PATH_NOT_EXPORTED in Node.js

Problem Statement

The ERR_PACKAGE_PATH_NOT_EXPORTED error occurs when Node.js attempts to access a module subpath that isn't defined in a package's exports field. This typically happens when:

  • You're using an invalid import path that references non-public modules
  • Package dependencies have changed their public API structure
  • There are version incompatibilities between dependencies
  • Your IDE incorrectly auto-imported internal paths

In the original example:

js
const OrbitDB = require('orbit-db');
// Triggers:
// Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './cid' is not defined by "exports"

This error indicates that multiformats/cid was accessed, but the multiformats package doesn't export this subpath in its package.json exports field - likely due to version changes or module system issues.

Solutions

1. Check and Correct Import Paths

This is the most common fix. Import paths should reference public API entry points only:

js
// INCORRECT - imports internal module
import { getFirestore } from "firebase-admin/lib/firestore";

// CORRECT - uses public API
import { getFirestore } from "firebase-admin/firestore";

Common issues:

  • IDEs sometimes add extra paths like /lib, /src, or /index
  • Verify imports match package documentation

2. Resolve Dependency Incompatibilities

Downgrade incompatible packages

bash
# For multiformats issue with orbit-db
npm install multiformats@9.9.0

# Alternative: Downgrade orbit-db to use CommonJS-compatible dep
npm install orbit-db@0.26.0

Verify fixed versions in package.json

json
{
  "dependencies": {
    "orbit-db": "^0.26.0",
    "multiformats": "9.9.0"
  }
}

Compatibility Issues

Many "Package subpath" errors stem from:

  1. ESM/CommonJS Mismatches: Packages switched to ESM while dependencies remain CommonJS
  2. Breaking API Changes: Package removed old subpaths in new versions
  3. Transitive Dependency Conflicts: Mismatched versions of sub-dependencies

3. Reinstall Dependencies

Fix corrupted installations by cleaning modules:

bash
rm -rf node_modules/ package-lock.json
npm install

4. Dynamic Import for ESM Packages

If dealing with ESM-only packages in CommonJS:

js
// Replace static require with dynamic import
const { CID } = await import('multiformats/cid');

ESM Interoperability

  • Use import() for non-static loads
  • Set "type": "module" in package.json for ESM projects
  • Avoid mixing require and ESM syntax

5. Environment Variable Workaround

For some environments with configuration issues:

json
{
  "scripts": {
    "start": "SET NODE_OPTIONS=--openssl-legacy-provider && react-scripts start"
  }
}

Use with Caution

Environment flag solutions are temporary workarounds - resolve the root cause when possible

Explanation

The exports field in package.json was introduced in Node.js 12+ to:

  • Restrict public API interface
  • Improve package encapsulation
  • Support conditional exports
json
{
  "exports": {
    ".": "./main.js",
    "./feature": "./feature.js" // Only exported paths
  }
}

Key reasons for ERR_PACKAGE_PATH_NOT_EXPORTED:

  1. ESM Transition: Packages like multiformats switched to ESM (import/export syntax)
  2. Subpath Removal: Packages deprecated internal paths in major releases
  3. Dependency Conflicts: Transitive dependencies request incompatible paths

Best Practices

  1. Update Dependencies: Ensure compatible versions (npm outdated)
  2. Review Imports: Never import /lib/, /internal/, or /src/ paths directly
  3. Check Support: Verify package compatibility with your Node version
  4. Review Error Stack: Match paths against package.json exports

Conclusion

To resolve ERR_PACKAGE_PATH_NOT_EXPORTED:

  1. Verify all import paths reference valid exports
  2. Adjust package versions for compatibility
  3. Use dynamic imports for ESM packages in CommonJS
  4. Reinstall packages to fix corruption
  5. Update modules for ESM compatibility

For orbit-db specifically, either:

  • Downgrade multiformats to v9.9.0
  • Downgrade orbit-db to v0.26.0
  • Check for an updated orbit-db version with migration support

Deprecation Note

Solutions involving --openssl-legacy-provider or await import workarounds should be temporary. Migrate to updated package versions for long-term stability.