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:
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:
// 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
# 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
{
"dependencies": {
"orbit-db": "^0.26.0",
"multiformats": "9.9.0"
}
}
Compatibility Issues
Many "Package subpath" errors stem from:
- ESM/CommonJS Mismatches: Packages switched to ESM while dependencies remain CommonJS
- Breaking API Changes: Package removed old subpaths in new versions
- Transitive Dependency Conflicts: Mismatched versions of sub-dependencies
3. Reinstall Dependencies
Fix corrupted installations by cleaning modules:
rm -rf node_modules/ package-lock.json
npm install
4. Dynamic Import for ESM Packages
If dealing with ESM-only packages in CommonJS:
// 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:
{
"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
{
"exports": {
".": "./main.js",
"./feature": "./feature.js" // Only exported paths
}
}
Key reasons for ERR_PACKAGE_PATH_NOT_EXPORTED
:
- ESM Transition: Packages like
multiformats
switched to ESM (import
/export
syntax) - Subpath Removal: Packages deprecated internal paths in major releases
- Dependency Conflicts: Transitive dependencies request incompatible paths
Best Practices
- Update Dependencies: Ensure compatible versions (
npm outdated
) - Review Imports: Never import
/lib/
,/internal/
, or/src/
paths directly - Check Support: Verify package compatibility with your Node version
- Review Error Stack: Match paths against package.json exports
Conclusion
To resolve ERR_PACKAGE_PATH_NOT_EXPORTED
:
- Verify all import paths reference valid exports
- Adjust package versions for compatibility
- Use dynamic imports for ESM packages in CommonJS
- Reinstall packages to fix corruption
- 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.