Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TypeScript cannot find a package subpath that its exports field defines

Errorerror 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.

  • Applies to TypeScript 5.9, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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'
  2. 2Match what you see, then jump to its fix

    1. 1The import works in Node.js at runtime but tsc cannot find itmoduleResolution is node10, which does not read exportsFix
    2. 2moduleResolution is already node16, nodenext or bundler but exports is still ignoredresolvePackageJsonExports was turned offFix
    3. 3ERR_PACKAGE_PATH_NOT_EXPORTED at runtime, or TS2307 only for that subpathThe subpath is not exported by the packageFix

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.json

2Fix

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 --noEmit

Confirmed by typescriptlang.org

Did this fix it?

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*.json

2Fix

Remove the line or set it to true.

{
  "compilerOptions": {
    "resolvePackageJsonExports": true
  }
}

3Verify

Type-check again.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

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 --noEmit

Confirmed by nodejs.org

Did this fix it?

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:

Related fixes

Back 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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.