-
Notifications
You must be signed in to change notification settings - Fork 406
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
Package cannot be imported for certain TypeScript configurations #604
Comments
After having encountered this with one of my packages I can confirm that just removing the |
Sorry for taking this long to respond to this bug report. You're right in saying that this whole topic is a big can of warms and I haven't felt like sticking my toes into it again, as each time I do I feel like I get smacked in the face :) I tried the approaches you suggested, but I'm not sure that they worked out. I released multiple beta-versions of SQL Formatter:
Trying all these versions against your test repository I wasn't really happy with none of them. Removing the
When I also include the "types" field to exports (as in beta-3) I get a more descriptive error:
Adding the So I'm not really sure about the best approach here. Not having the |
Interestingly though for my own purposes I'm importing |
Thanks for following up on this, and for taking the time to play around with solutions! To respond to them:
If you're open to it, I'd be willing to open a PR attempting to replace |
Sure. Feel free to experiment and make a pull request. I'm not too worried about giving up separate files as long as one can still only import what he wants and rely on tree-shaking to eliminate the unused code. |
I gave How attached are you to the CJS code (currently |
Thanks for taking a look.
We can throw the
Not particularly attached. It has again been convenient to use it through |
@nwalters512 Thanks for your efforts. I merged in your PR and released as One thing that caught my eye is that we're outputting I will be going away from computer for a week or so. So I probably won't place it into an official release just yet. But do play around with it and see if there's anything still missing. |
Describe the bug
I apologize in advance for filing this; ESM/CJS/TypeScript compatibility is a horrific can of worms.
Importing this package in a TypeScript project that uses
"module": "Node16"
fails when the project itself is not configured to use ESM (that is,package.json
does not contain"type": "module"
).I've created a reproduction here: https://github.com/nwalters512/sql-formatter-typescript-repro. Instructions are in the readme.
Expected behavior
Given that
sql-formatter
provides both ESM and CJS implementations, I would expect to be able to import the CJS version in this situation.Actual behavior
TypeScript produces the following error:
Note that things still work correctly at runtime; the presence of the
types
field combined with"type": "module"
in this package confused TypeScript enough that it doesn't realize that there is a completely valid CJS implementation. I considered filing an issue upstream on TypeScript itself, but this was already discussed on microsoft/TypeScript#54263, which was closed without any indication that TypeScript itself would fix this (even though I think they should).I believe the way to fix this would be to drop the
"types"
field from"exports"
and instead place*.d.ts
/*.d.cts
files adjacent to the files that are referenced by the"import"
and"require"
exports inpackage.json
. This way, TypeScript will automatically discover the correct types based on the particular conditions that it's using to resolve the implementation files.The text was updated successfully, but these errors were encountered: