Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Compilation issue with TypeScript 5.2.2 #204

Closed
2 tasks done
frangio opened this issue Oct 24, 2023 · 9 comments · Fixed by #205
Closed
2 tasks done

Compilation issue with TypeScript 5.2.2 #204

frangio opened this issue Oct 24, 2023 · 9 comments · Fixed by #205

Comments

@frangio
Copy link
Contributor

frangio commented Oct 24, 2023

Describe the bug

I'm seeing the following compiler error when using TypeScript 5.2.2:

$ npx tsc
src/utils.ts:3:35 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("abitype")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts' or create a local package.json file with `{ "type": "module" }`.

3 import type { AbiParameter } from 'abitype';
                                    ~~~~~~~~~

I have these settings in my tsconfig.json:

"module": "node16",
"moduleResolution": "node16",

I noticed that abitype publishes CommonJS files with a .js extension, even though in package.json it says "type": "module". Manually changing "type": "module" to "type": "commonjs" fixes the compiler error. This seems like an odd fix because ES modules are also published with a .js extension, however those are perhaps hardcoded to be interpreted as ES modules because they are reached through the explicit "import" entrypoints?

Link to Minimal Reproducible Example

The playground doesn't work with ES Modules.

Steps To Reproduce

No response

Package Version

0.10.1

I don't see the same issue with 0.9.10.

TypeScript Version

5.2.2

Anything else?

No response

Validations

@Raiden1411
Copy link
Collaborator

Could you share a reproducible link to your tsconfig and package.json?

@Raiden1411 Raiden1411 added the needs reproduction Needs a minimal reproducible example before further investigation label Oct 26, 2023
@github-actions
Copy link
Contributor

Hello @frangio. Please provide a minimal reproduction using a TypeScript Playground or GitHub repository. Issues marked with "needs reproduction" will be closed if they have no activity within 3 days.

@frangio
Copy link
Contributor Author

frangio commented Oct 26, 2023

https://github.com/frangio/repro-abitype-error

See error with npx tsc

@Raiden1411 Raiden1411 removed the needs reproduction Needs a minimal reproducible example before further investigation label Oct 27, 2023
@Raiden1411
Copy link
Collaborator

Raiden1411 commented Oct 28, 2023

We had this same problem when we started to use TS 5.2.2. This was because of a change in typescript's module/moduleResolution that you can check here.

To have it run as cjs your tsconfig.json must now be like this:

"module": "CommonJS",
"moduleResolution": "Node10"

Some more details of the changes in typescript can be found on this PR and this issue.

Hope this help!

@frangio
Copy link
Contributor Author

frangio commented Oct 28, 2023

Updating abitype so that it says "type": "commonjs" in the package.json fixes this. Do you see a reason why abitype shouldn't make this change?

@tmm
Copy link
Member

tmm commented Oct 28, 2023

What we need to do is to emit package.json files to each dist directory (e.g. dist/esm and dist/cjs) with the specific module type defined there. Can see how this works in Viem.

@frangio
Copy link
Contributor Author

frangio commented Oct 29, 2023

Oh, that actually seems to be the case already:

https://github.com/wagmi-dev/abitype/blob/32cf12dcece9f7ef0d3e06376b81feb52dc801be/packages/abitype/package.json#L9

This made me realize that the error only happens if my project has "type": "commonjs" (explicitly or implicitly as the default). It also happens with normal import (as opposed to import type), but in this case the error is obviously a TypeScript bug where it doesn't seem to consider the exports field in package.json? In the case of import type it actually also sounds like a TypeScript bug to me. I will look into it more and report it upstream...

Closing this issue. FWIW, I don't think there is any possible workaround that abitype could implement.

@frangio frangio closed this as completed Oct 29, 2023
@frangio
Copy link
Contributor Author

frangio commented Oct 29, 2023

After some more experimentation I've found that the error is caused by having "type": "module" in a place that affects abitype's .d.ts files. Now I understand that this is what @tmm was suggesting to change, by removing that and adding it instead in dist/esm/package.json.

Copy link
Contributor

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest ABIType version. If you have any other comments you can create a new discussion.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 13, 2023
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants