Can't import my module if importing module has moduleResolution: bundler enabled

908 Views Asked by At

I have a module A that gets imported by a module B. Module B has the following tsconfig.json:

{
  "compilerOptions": {
    "strict": true,
    "module": "esnext",
    "moduleResolution": "bundler",
  }
}

If I try to import module A with this config, I get the following error:

Could not find a declaration file for module 'moduleA'. 'node_modules/moduleA/index.mjs' implicitly has an 'any' type.
There are types at 'node_modules/moduleA/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'moduleA' library may need to update its package.json or typings.

If I change moduleResolution to "node" however, everything works fine. This is the package.json of module A:

{
    "name": "ModuleA",
    "version": "1.0.0",
    "main": "index.mjs",
    "module": "index.mjs",
    "types": "index.d.ts",
    "type": "module",
    "files": [
        "index.cjs",
        "index.mjs",
        "index.d.ts"
    ],
    "exports": {
        ".": {
            "import": {
                "types": "./index.d.ts",
                "default": "./index.mjs"
            },
            "require": {
                "types": "./index.d.ts",
                "default": "./index.cjs"
            }
        }
    },
    "engines": {
        "node": ">=18.0.0"
    },
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    }
}

I've checked that all the files that package.json exist and are in the right path and they are. I tried to look up documentation around the "bundler" mode, but could not find much as this is a new feature. Anyone know what might be causing this?

Also in case it might be relevant, this is the format of the index.d.ts in module A:

type A = {
    someField: someType;
    ...
};

type B = {
    someOtherField: someType;
    ...
};

// More types here
export {
    A, B, // More types 
};


1

There are 1 best solutions below

1
On

Try extending compilerOptions with "customConditions": ["import"]: https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#customconditions.