-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Import failing after upgrade to v5 #306
Comments
Indeed, it would appear I'm not the only one. This is what I get when I try to look up the package in Bundlephobia: https://bundlephobia.com/package/change-case This is fine: https://bundlephobia.com/package/[email protected] |
It’s a duplicate of #304. Unfortunately Bundlephobia doesn’t support ESM so you can’t rely on that result. Please see the note in the READMEs of the packages on how to enable ESM, I don’t support CommonJS anymore. |
I also have this issue with an angular 16 project. Changing the moduleResolution to |
If you have a reproduction I can look at I can help you debug, but that sounds more like a bug in angular. Typescript support for ESM is just following https://www.typescriptlang.org/docs/handbook/esm-node.html |
Might also be a problem with change-case could add it, it won’t do any harm to the project I guess, and it would work for those old typescript projects as well, probably a lot of projects not changing the moduleResolution that often. Or perhaps hint at the solution in the docs for anyone trying to figure it out? I noticed it in a project that uses a lot of other esm-packages, but types for change-case didn’t work. |
I've found that I needed to do three things to get it just about working:
However all of our tests now fail because Karma doesn't support ESM imports in their conf.js and ESM doesn't support |
We use Vuex 4 which doesn't support |
@blakeembrey this esm change breaks my entire nx monorepo... |
@JakeAi No it doesn’t. You aren’t being forced to upgrade. That’s the point of a breaking change version bump, it introduces incompatible changes with the previous version. I no longer intend to maintain CommonJS packages. |
No, adding it won’t fix typescript. I’d need to create a CommonJS compatible version of the package, therefore shipping two versions. I did that for the last few years with the previous major version, going forward it’ll be ESM only. |
Also, there is documentation added to each of the packages READMEs, can you let me know if that is insufficient? |
I create a blank nx repo, with an angular app. Import your library. And change-case package is not found. I add
|
I’m sorry, it’s ESM only now and apparently that doesn’t work properly with Angular. I’d suggest filing a ticket with them, I’m not the only person building packages with ESM nowadays.
I think I misinterpreted this to assume you meant an existing repo. Of course, you can still install the previous major release. You don’t need the latest version. |
@JakeAi If there’s anything you can share with me to reproduce I can file an issue on your behalf if you’d prefer. I’m not familiar with NX or Angular. |
It could be related to something like this? nrwl/nx#18801. Or maybe this? nrwl/nx#15464. TL;DR is that it seems like NX is doing some sort of “magic” that just doesn’t work properly with ESM + TypeScript here. Unfortunately I’m not too familiar with this, I typically avoid using tools like this and use direct |
Also nrwl/nx#11335, seems like it’s been over a year and people can’t run ESM using NX 🤷 It doesn’t sound promising. |
No. I was not referring to the ESM switch. I was referring to adding the module field in package.json which would fix this issue for all projects using a tsconfig with moduleResolution set to node which probably is a lot. When set to node ts don't understand the exports field in package.json according to my understanding. Which gives the problem described in this issue. |
This doesn’t fix typescript, it’s not how typescript works. How did you get this to work? |
There’s nowhere in TypeScript’s module resolution strategies that it even uses the module field, as far as I’m aware. Some bundler may consider it, but what TypeScript actually uses in the old node module resolution strategy is the types/typings field. And that is expected to be the CommonJS version of the package. If I added that back, it wouldn’t work properly with imports because it’d allow CommonJS in TypeScript to work differently depending on the users synthetic import options. And then I’d just have a different set of issues being filed asking for CommonJS to work. |
My project still doesn't work, it's been a whole day trying to fix this. :( |
Don’t know, might have changed something else aswell. Tried in a mini repro: package.json {
"type": "module",
"devDependencies": {
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
"dependencies": {
"change-case": "^5.0.2"
},
"scripts": {
"start": "ts-node-esm index.ts"
}
} tsconfig.json {
"compilerOptions": {
"target": "es2016",
"module": "ESNext",
"moduleResolution": "node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
index.ts import * as changeCase from "change-case";
console.log(changeCase.camelCase("what is happening")); This above e.g. will say types are missing. Changing "module": "NodeNext",
"moduleResolution": "NodeNext", Will make it work. And manually editing change-cases package.json and adding: "types": "dist/index.js", Will also make it work. There might (and are) more ways to make it work. I used another combo in our bigger project. Some other esm packages seem to have both ”types” and ”exports” e.g.: |
@emanuel-lundman Thanks for the references. FWIW I wanted to check why they added |
Specifically:
(The gist link is the same one I've added to the package READMEs to clarify this too). |
I don't quite follow if we're talking about the same thing. But that's alright. Thanks for looking in to it. 🙂 Just to be clear, I wasn't saying there's a need to revert the exports field in package.json to make it work. But adding the types field seemed during my test to make it much more compatible with more moduleResolutions than node16. Switching to moduleResolution node16 can be a big hassle for an old large project. Adding extensions to all imports maybe in hundreds of files etc. TLDR; The package.json I referenced in the sindresorhus package e.g. was for the next major version (6.0.1) and he had reverted to using ...
"type": "module",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=16"
},
... But of course it's up to you to decide. Wish you all the best. 🙂 |
Yes, I agree. It works for TypeScript, but it won't work for node projects using CommonJS. I vaguely understand wanting to allow people to just upgrade and it work, but to be fair it'd just break elsewhere. I.e. the error would turn into a node.js issue trying to
It doesn't look like he reverted again, just forgot to revert before 6.x 🤷 Easy to forget I suppose, but I see what you mean now. I guess there's other reasons he might have left it alone, I've found other ecosystem interop issues too (i.e. NPM package pages also don't support ESM types exports). I'll put my effort to improving the ecosystem rather than hacking it in this package, hopefully we can make it so this is less confusing on future TypeScript versions. |
@emanuel-lundman I thought more about your point overnight and added
In another issue someone shared this and it was very helpful way to debug TypeScript/ESM support: https://arethetypeswrong.github.io |
Package has been working fine with v4.1.2
However, after upgrading to v5, I get the following error:
My import command is just
import { camelCase } from 'change-case'
, which worked fine before. 🤷♂️The text was updated successfully, but these errors were encountered: