TypeScript Cannot Find a Package’s Types - Fix "exports"/typesVersions in CI
When a package ships an exports map, TypeScript resolves its types through that map under moduleResolution: node16/nodenext/bundler. If the package omits a types export condition (or its typesVersions does not match), the type-check fails to find declarations even though the JS resolves fine.
What this error means
A type-check or build fails with "Could not find a declaration file for module" or "has no exported member", only under modern moduleResolution. The runtime import works; TypeScript cannot follow the package’s exports/typesVersions to its .d.ts.
error TS7016: Could not find a declaration file for module 'some-pkg'.
'/app/node_modules/some-pkg/dist/index.js' implicitly has an 'any' type.
There are types at '/app/node_modules/some-pkg/dist/index.d.ts', but this result
could not be resolved under your current 'moduleResolution' setting.Diagnose it: what is different about the runner?
A build that passes locally and fails on a runner differs in a small number of predictable ways. Check those before changing build configuration, because the build config is usually not the thing that changed.
- run: |
node --version && npm --version
echo "NODE_ENV=$NODE_ENV CI=$CI"
nproc && free -h && df -h /
ls -la node_modules/.bin | headCommon causes
The package’s exports map lacks a types condition
Under node16/nodenext/bundler resolution, TypeScript looks for a types (or import/require with co-located .d.ts) condition in exports. A package missing it cannot have its types found through the map.
typesVersions or moduleResolution mismatch
A typesVersions block that does not match your TypeScript version, or a tsconfig moduleResolution that disagrees with how the package publishes types, breaks resolution.
How to fix it
Align moduleResolution and add the missing condition
Set a resolution mode the package supports; for your own package, add a types condition to exports.
// tsconfig.json - match how the dep ships types
{ "compilerOptions": { "moduleResolution": "bundler" } }
// for a package YOU publish, expose types in exports:
"exports": { ".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" } }Work around an unfixable upstream package
- Check whether an
@types/<pkg>package exists and install it. - Add a local ambient
declare module "some-pkg"declaration if no types are published. - File an issue upstream to add the
typesexport condition.
The three that account for most of them
- Case sensitivity. Linux runners are case sensitive, macOS is not. An import with the wrong case resolves locally and fails in CI.
- Out of memory. Exit code 137 is a SIGKILL from the kernel, not a build error. Raise
--max-old-space-sizeor use a larger runner. - devDependencies pruned.
NODE_ENV=productionmakesnpm ciskip devDependencies, so the build tool itself goes missing. Set it after install, not before.
How to prevent it
- Choose a moduleResolution matching how your deps ship types.
- In your own packages, include a
typescondition in exports. - Add ambient declarations only as a temporary shim.