blakeembrey / change-case

Convert strings between camelCase, PascalCase, Capital Case, snake_case and more
MIT License
2.21k stars 92 forks source link

Add `module` and `types` to package.json to make it resolvable by TypeScript #303

Closed emeryao closed 9 months ago

emeryao commented 9 months ago

for now the package of change-case cannot be resolved by typescript with tsconfig "moduleResolution": "node" since the lack of module and types field in package.json

emeryao commented 9 months ago

there is a kind of compatibility issue with the "moduleResolution": "node" in tsconfig.json and the exports field in package.json

if I change the moduleResolution to bundler or nodenext the package of change-case can be resolved as expected by TypeScript but updating tsconfig will break my entire project since all other packages will not be resolved as before

so by adding these two lines of code to package.json solves this issue

blakeembrey commented 9 months ago

The node module resolution isn’t compatible with ESM, you need to be using Node16, NodeNext, or Bundler. I won’t be merging this because it only happens to work today since this is a single file package. If I was ever to refactor this would break.

It’s an unfortunate situation for TypeScript but I’ll add some docs to the README instead. Node.js ESM isn’t compatible with any of the old module resolutions.

blakeembrey commented 9 months ago

but updating tsconfig will break my entire project since all other packages will not be resolved as before

Can you elaborate more on this? What broke? Are you currently using CommonJS or ESM or a bundler? Also, module in package.json isn’t a node feature, so I assume you’re specifically trying to make this work for a bundler and without changing the config to bundler?

Node16 adds ESM support to the existing Node resolution, it should not break anything that previously worked because Node16 still resolves the same thing you’re trying to add in this PR when the package is CommonJS.

blakeembrey commented 9 months ago

Here’s a similar report in TypeScript: https://github.com/microsoft/TypeScript/issues/55104. It links to an issue in another repo that has a good explanation of the situation: https://github.com/typescript-eslint/typescript-eslint/issues/7284

As TypeScript ESLint has done, I’m no longer supporting non-ESM versions of the package. To upgrade you will need to enable support for ESM, both in your environment (e.g. node, bun, etc) and TypeScript (with module resolution).

Being unable to upgrade is unfortunate, but I’m not forcing anyone to upgrade. The previous version has worked for years and will continue to work for as long as CommonJS is supported (hopefully forever).

emeryao commented 9 months ago

fair enough 😃 my project is a angular@16 repo which defaults moduleresolution to node when i update it to bundler the import of @angular/commom/locales/en_US failed to be resolved

it's sure a issue of the @angular/common package and i will find a workaround

thanks for you kind replies 😃