TypeScript issue: `moduleResolution:NodeNext` doesn’t work with this library
Opened this issue · 1 comments
rauschma commented
The problem
tsconfig.json
:
"module": "NodeNext",
"moduleResolution": "NodeNext",
TypeScript code:
import { parse } from '@adobe/css-tools';
Result – error from tsc:
- Could not find a declaration file for module '@adobe/css-tools'. '~/my-lib/node_modules/@adobe/css-tools/dist/index.mjs' implicitly has an 'any' type.
- There are types at '~/my-lib/node_modules/@adobe/css-tools/dist/types.d.ts', but this result could not be resolved when respecting package.json "exports". The '@adobe/css-tools' library may need to update its package.json or typings.
Solutions
Quickest fix – change package.json
:
"exports": {
"import": "./dist/index.mjs",
"types": "./dist/types.d.ts",
"require": "./dist/index.cjs"
},
In the long run, you don’t need "main"
, "module"
and "types"
(anywhere!) anymore, as these are superseded by "exports"
and modern TypeScript .d.ts
resolution. You only have to rename:
mv types.d.ts index.d.mts
Possibly useful
- TypeScript documentation on package
"exports"
: https://www.typescriptlang.org/docs/handbook/modules/reference.html#packagejson-exports - This tool was recommended to me as a linter for packages with TypeScript types (haven’t used it yet though): https://github.com/arethetypeswrong/arethetypeswrong.github.io
- I’m not sure how well
.d.mts
works with older TypeScript versions. There, you may have some success with thepackage.json
property"typesVersions"
(but in general it’s better if you don’t use it and only rely on"exports"
): https://2ality.com/2021/06/typescript-esm-nodejs.html