Skip to content
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

Support for ESM in TypeScript projects #3385

Open
fvictorio opened this issue Nov 30, 2022 · 50 comments
Open

Support for ESM in TypeScript projects #3385

fvictorio opened this issue Nov 30, 2022 · 50 comments
Assignees
Labels
effort:difficult status:blocked Blocked by other issues or external reasons type:feature Feature request

Comments

@fvictorio
Copy link
Member

fvictorio commented Nov 30, 2022

Please comment and/or upvote this issue if you need support for ESM in TypeScript projects. If possible, tell us more about your use case and why you need this and cannot just transpile to cjs.

UPDATE: We have an experimental version working

See this comment to learn how to use an experimental version of this: #3385 (comment)

@github-actions
Copy link
Contributor

This issue is also being tracked on Linear.

We use Linear to manage our development process, but we keep the conversations on Github.

LINEAR-ID: b31f8c05-52a3-44ca-b22e-10ec21278d4c

@matthewlilley
Copy link

matthewlilley commented Dec 6, 2022

@fvictorio we use wagmi/core for some scripts which only supports ESM. Had to convert all tests to .js and extract chunks of scripts into another package for now to deal with this.

sushi-labs/sushiswap@ccd68f9

@DenisGorbachev
Copy link

I'm using https://github.com/sindresorhus/execa, which is ESM-only.

@anteqkois
Copy link

I can't run task when use new TypeScript:

"module": "NodeNext",
"moduleResolution": "NodeNext",

Obviously when i delete "type": "module" from package.json it's start work, but i must use this setting.

Error from terimnal:
image

@talentlessguy
Copy link
Contributor

Suggestion for TS ESM implementation:

ts-node has a ts-node/esm loader hook. Also mts extension could be used but I'm not sure if that's needed.

@paulmillr
Copy link

Copying @alcuadrado comments from monorepo:

Nop, unfortunately Hardhat's core depends on CJS pretty heavily.

The reason for that is that our plugin system is dependent on the order of require() that users make to plugins. In ESM the order of imports is not guaranteed (and does change in practice).

What about await import? You can do this one after another.

We need to rework our plugin system for it to fully support ESM.

I'm starting to think that maybe we should move to esm-only when it's properly supported by ts-node or some similar project. But note that even after that, the migration would be a pretty large project, as every plugin will need to be adapted.

If you keep supporting two module systems, ESM modules would be able to import common.js code - which uses old sequential requires and is cool.

@alcuadrado
Copy link
Member

What about await import? You can do this one after another.

Oh, that may be a great idea actually! I think now ESM in node.js means/implies that you have top-level-await. This could work! Thanks !

@alex-mccreary
Copy link

Upvote; we are using ESM in our TypeScript project because of a dependency requirement.

@pynixwang
Copy link

pure esm package is coming.

@thevolcanomanishere
Copy link

I am looking to integrate Playwright with Hardhat + Wagmi in order to automate frontend testing that involves connecting to a hardhat node :)

@kostysh
Copy link

kostysh commented Apr 14, 2023

Upvote; we are using ESM in our TypeScript project

@highskore
Copy link

pure esm package is coming.

do u know when? 🧐

@robwilkes
Copy link

upvote, many packages are ESM only now, I have to install older versions for compatibility. all my projects and code is ESM now, except for hardhat.

@CJ42
Copy link

CJ42 commented Jun 7, 2023

Upvoting as well. We have constants values defined in a constants.ts / constants.js](https://github.com/lukso-network/lsp-smart-contracts/blob/develop/constants.ts) file in the LUKSO lsp-smart-contracts. These are part of the [@lukso/lsp-smart-contracts` npm package and are important values to be consumed by projects that use our smart contracts package.

https://github.com/lukso-network/lsp-smart-contracts

In our case, this is problematic as any Hardhat Typescript project that has the @lukso/lsp-smart-contracts as a dependency cannot import these necessary constant values.

The only workaround we found was in this PR by @richtera: lukso-network/lsp-smart-contracts#601.

image

https://github.com/lukso-network/lsp-smart-contracts/pull/601/files

The file in the folder looks like this:

image

ES Lint raises a warning, but we are still able to import as shown below.

import { INTERFACE_IDS } from "@lukso/lsp-smart-contracts/dist/constants.cjs.js";

const test = INTERFACE_IDS.LSP0ERC725Account;

@vinerz
Copy link

vinerz commented Jul 9, 2023

Following this issue. Would help a lot to get rid of some workarounds for ESM/CJS interop.

@kanej kanej moved this from Backlog to Blocked in Hardhat May 17, 2024
@sim31
Copy link

sim31 commented Jun 5, 2024

I experience the shifted line numbers in stack traces as well. Thanks @fullkomnun for your report, saved me time from trying these mitigations myself.

@pogilvie
Copy link

It's 2024 and I'd like to start new projects with Typescript and ESM support. It would be create to support a runtime like Bun or Deno which supports these natively out of the box.

@Ahmedborwin
Copy link

Ahmedborwin commented Jun 13, 2024

I am trying to migrate a scaffold-eth project to allow for hardhat ESM support. Repo: https://github.com/Ahmedborwin/cryptoSoccer.

I have followed the instructions listed by @schaable but I still get the much-loved HH19 error when I try to import hardhat tasks (that I got from the chainlink functions boilerplate) to my hardhat.config.cts.

The tasks folder is made up of .js scripts that are common modules. Not sure why importing them is causing the error in the first place. I tried converting them to .cjs but still no luck.

Anyone have any idea how I can make these hardhat tasks available to my hardhat typescript project?

If this question is badly worded, please be patient with me. I am learning. Thank you

@damianobarbati
Copy link

Is there any news on the topic from the team? Should the user expect the project to be stuck on the old CJS or was any progress made with ESM? Thanks guys for the effort.

@kanej
Copy link
Member

kanej commented Jul 15, 2024

Is there any news on the topic from the team? Should the user expect the project to be stuck on the old CJS or was any progress made with ESM? Thanks guys for the effort.

The team is currently focused on the next version of Hardhat, which will support ESM.

@0xVinCal
Copy link

It would be great if the next version could be used with bun

@ppciesiolkiewicz
Copy link

ppciesiolkiewicz commented Oct 22, 2024

Just letting you know
Screenshot 2024-10-22 at 12 43 35


Needed to use permissionless lib from Pimlico

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort:difficult status:blocked Blocked by other issues or external reasons type:feature Feature request
Projects
Status: Blocked
Development

No branches or pull requests