TypeScript cannot find a package subpath that its exports field defines
error TS2307: Cannot find module 'pkg/subpath' or its corresponding type declarations.Short answer
TypeScript only reads a package's exports field in node16, nodenext and bundler resolution modes. The old node10 mode ignores it. Switch moduleResolution (and pair module correctly), then check that the package really exports that subpath and lists a types condition first.
Fix it fast
1Find out which cause you have
Print the module settings tsc actually uses, since a base config may set them for you.
npx tsc --showConfig | grep -E 'module|moduleResolution'2Match what you see, then jump to its fix
- 1
The import works in Node.js at runtime but tsc cannot find itmoduleResolution is node10, which does not read exportsFix - 2
moduleResolution is already node16, nodenext or bundler but exports is still ignoredresolvePackageJsonExports was turned offFix - 3
ERR_PACKAGE_PATH_NOT_EXPORTED at runtime, or TS2307 only for that subpathThe subpath is not exported by the packageFix
- 1
What does the error look like?
error TS2307: Cannot find module 'pkg/subpath' or its corresponding type declarations.ERR_PACKAGE_PATH_NOT_EXPORTED
Cause 1 of 3
moduleResolution is node10, which does not read exports
1Check
The TypeScript modules reference marks package.json exports as unsupported in node10, and says node10 should no longer be used. Look for moduleResolution node or node10, or a module value that implies it.
grep -n -E 'moduleResolution|"module"' tsconfig.json2Fix
For a Node.js app use module and moduleResolution nodenext. For code that goes through a bundler use moduleResolution bundler. Node16 and nodenext must be paired with a module of node16, node18, node20 or nodenext.
{
"compilerOptions": {
"module": "nodenext",
"moduleResolution": "nodenext"
}
}3Verify
Type-check again.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 2 of 3
resolvePackageJsonExports was turned off
1Check
The option defaults to true in those three modes. Someone may have set it to false.
grep -rn resolvePackageJsonExports tsconfig*.json2Fix
Remove the line or set it to true.
{
"compilerOptions": {
"resolvePackageJsonExports": true
}
}3Verify
Type-check again.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 3 of 3
The subpath is not exported by the package
1Check
When exports is defined, every subpath not listed is unavailable to importers. Read the exports map of the package and compare it with your import.
node -e "console.log(require('./node_modules/pkg/package.json').exports)"2Fix
Import a subpath that is listed, or ask the package author to export it. In a package you own, add the subpath and put the types condition first so TypeScript finds the declaration.
"exports": {
".": {
"types": "./index.d.ts",
"default": "./index.js"
}
}3Verify
Import the listed subpath and run the type check.
npx tsc --noEmitConfirmed by nodejs.org
How do I stop it coming back?
- Use nodenext for Node.js code and bundler for bundled code; avoid node10 in new projects.
- In your own packages, put the types condition first in every exports entry.
- Test imports of your package from a consumer project before you publish.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
TypeScript cannot find '@angular/platform-browser' in an Angular project
Cannot find module '@angular/platform-browser' or its corresponding type declarations.
TS2307 means TypeScript could not turn the import into a file.
3 causes, each with a fixBack end
Angular build fails with TS2707 or "has no exported member" after installing a library
Module '"@angular/core"' has no exported member
These errors mean a library was compiled by a newer Angular than your app uses.
3 causes, each with a fixBack end
cdk synth says --app is required
--app is required either in command-line, in cdk.json or in ~/.cdk.json
The CDK CLI needs a command that runs your app and finds it in three places: the --app option, cdk.json in the project folder, or cdk.json in your home folder.
3 causes, each with a fixBack end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
Mocha or Node cannot find ts-node/register
Error: Cannot find module 'ts-node/register'
Node resolves ts-node/register from your project's node_modules, so the package must be installed there, next to typescript.
2 causes, each with a fixBack end
TypeError: Class constructor cannot be invoked without 'new' in compiled TypeScript
TypeError: Class constructors cannot be invoked without 'new'
Node.js throws this when a native class is called like a plain function.
2 causes, each with a fix