I'm creating a supertest alternative called tepper. I'm trying to publish the package both as ESM and CJS. The problem I'm having is that a consumer of the library isn't able to find the type definitions.
In order to test it, I've created a script (you can find it here), that generates a brand-new NestJS project, and tries to use the library by installing it locally using a relative path (npm install --save-dev ../../../..
)
The problem is that I'm having an TS2307: Cannot find module 'tepper' or its corresponding type declarations
error (here you can see the error on Github Actions).
I've exported the types in package.json as I'm supposed:
{
"exports": {
".": {
"types": "./dist/types/tepper.d.ts",
"require": "./dist/cjs/tepper.js",
"import": "./dist/esm/tepper.js",
"default": "./dist/esm/tepper.js"
}
}
}
Thanks!
The key point is resolvePackageJsonExports
in tsconfig. Refer to https://www.typescriptlang.org/tsconfig#resolvePackageJsonExports.
In your project, it is not enabled because of values of moduleResolution
and module
. So you have 2 options,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"resolvePackageJsonExports": true,
types
field in tepper's main packages.json, which can avoid other things be affected due to changes of moduleResolution
and module
."types": "./dist/types/tepper.d.ts",