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

1

6

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 
};


Pedro answered 9/8, 2023 at 19:7 Comment(0)
P
-3

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

Proctology answered 5/10, 2023 at 9:0 Comment(1)
can you please elaborate? I looked at the referenced doc but I can't see how this is related and works, unfortunately.Dissect

© 2022 - 2024 — McMap. All rights reserved.