-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
error when using es modules [ERR_REQUIRE_ESM] #957
Comments
i tried renaming the config file to
|
@cruzdanilo can you provide instructions about how to reproduce this? |
just add |
I have the same problem. How did Uniswap solve this, they are using es6 modules |
@numismatic4 I think Uniswap uses typescript, not es6 modules. |
I am having this issue too. I have configured mocha to work with es6 imports and it is working mint. But I am having a hard time in converting this project to hardhat. When I do so and issue command ❯ npx hardhat test
Compiling 17 files with 0.8.0
Compilation finished successfully
An unexpected error occurred:
/home/david/Workspaces/Tachyonic/faster-than-light/test/YieldFarming.spec.js:1
import { MockProvider } from '@ethereum-waffle/provider'
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at /home/david/Workspaces/Tachyonic/faster-than-light/node_modules/mocha/lib/mocha.js:349:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/home/david/Workspaces/Tachyonic/faster-than-light/node_modules/mocha/lib/mocha.js:346:14) It looks module.exports = {
timeout: 20000,
require: ["esm"],
recursive: true,
exit: true
} |
|
It is now working as it should. I followed your suggestion and added yarn run v1.22.10
$ solhint 'contracts/**/*.sol'
Done in 2.73s.
yarn run v1.22.10
$ eslint .
Done in 0.86s.
yarn run v1.22.10
$ node --require esm node_modules/.bin/hardhat test
YieldFarming
✓ Ownership (3050ms)
1 passing (3s) |
I'm having a similar problem.. I'm using typescript and my package.json has "type": "module". I try compiling with
Full error trace
My hardhat.config.ts in same folder as package.json and tsconfig.json
|
AFAIK we don't support that yet. @alcuadrado can you confirm? |
I tried to use hardhat for the first time, and got this issue when using it with Edit: Looks like you're using the "main": "./dist/cjs/mypkg.js",
"module": "./dist/esm/mypkg.js", |
@fvictorio @alcuadrado did @Olyno solve it? Having the same issue, it is apparently impossible to "mix" a hardhat project with an ESM project. I sometimes feel like I am the first and only one programming ESM modules. :) Even the "npm esm" trick doesn't seem to help at all (unless I am doing it wrong):
Without esm it looks like this:
Note: my
|
Thanks for the suggestion, @Olyno. That's the right direction to fix this. Unfortunately this is not a high priority thing yet. This doesn't mean that we won't fix though. We need to do a lot of testing to make sure that Hardhat and its plugins properly support it. We don't want to end up in a situation where we partition the plugins community either. |
I've run into this too. It's pretty confusing, given that
|
Another alternative is just to use ts with a very permissive config. |
|
Are you using .ts as an extension? |
Yes. |
There goes my theory :( I guess using |
I still got the problem on my side using |
After so long, is it still not a priority? Typescript 4.7 is out and updating your typescript syntax to use |
I'm also concerned that this is marked as |
If you offer a bounty for this issue (using gitcoin or issuehunt), it would help solve this problem faster. I would even be ok to put myself on the issue as soon as the bounty is available. Without bounty, I can try to get on the issue later (by September). |
Hi everyone. We are working on having some initial support for ES Modules that doesn't require a new major version of Hardhat. The way this will work is that the config and anything required from it (plugins, files defining modules, etc.) will have to be in CJS, but you'll be able to use ESM for scripts and tests. The main issue tracking this effort is #3043. |
Heavily following this, |
with |
Yes, it works with |
@brozorec sorrry about that! I published a new version that should fix that issue. |
Well, to be honest, typescript+esm support is not quite there yet. I can't get scripts to run properly, and tests have to be |
actually what is the point of using esm if you use typescript, you already have access to the syntax |
@Sceat it's necessary if you want to use a package that is published as ESM. And since Sindre did that for most of his bazillion npm packages, some people are migrating to be able to use them. |
I agree with @Sceat. A typescript library has the opportunity to be compiled in both CJS and ESM. Many developers have already done this, and it's clearly not the most complicated thing. The big problem is the Hardhat base code, which is not favorable to this conversion. Many syntaxes are exclusive to CJS, especially the I have already started to solve several problems with Hardhat in one of my projects, Solidhat, but it only concerns the CLI for the moment. |
does |
@iamnader if you install the toolbox and then use the |
I'm getting the same thing on a nextjs project. Hardhat config is written in typescript, tests as well. I'm trying to import the hardhat runtime environment to perform programmatic verification of the smart contracts, but once imported into a nextjs api endpoint, it fails with the above error. Changing the hardhat config to js fixes the error and everything works fine, but then my tests will not be picked up since they're written in ts and moving them to js is not an option.
|
@mihaic195 yes, adding support for typescript + ESM will be hard, because there's not good support for that in |
I use hardhat and ESM without any problems. See https://github.com/TachyonicFinance/faster-than-light. The trick is calling |
@David-Kneel it works because you're not using typescript |
I know. It is interesting though how many people get confused about this still... https://stackoverflow.com/questions/70342333/how-do-i-use-esmodule-imports-with-hardhat-that-uses-commonjs |
@mihaic195, depending on your project requirements and timelines, it may make sense to transpile your typescript tests to js for the time being until hardhat provides full support to typescript + ESM, then switch back to typescript afterwards. |
I've been using this hardhat esm branch for the past week and noticed the same behavior described here https://github.com/NomicFoundation/hardhat/issues/957#issuecomment-1257640807 . Basically, although hardhat is installed, at some point, it seems to reset and any command to it shows the initial project generator screen. The only "fix" is to do the npm install of the esm branch again. Anyone else encountering this? |
@cnohara does that happen after you install something? |
@fvictorio I just tested and this definitely triggered hardhat to revert to its initial state. Actually, this also occurred when just uninstalling a package as well. I'm not sure if the install/uninstall are the only times this is triggered, but definitely true in my case. Is this intended behavior or did I do something wrong on my side? |
@cnohara can you share the |
Tadam !!!! I works like a breeze. And now I can use HH with the rest of my ESM codebase, which is a real pleasure because I work with SvelteKit. Thank you A LOT to all those who contribute to hardhat@esm!!! Having to maintain two different kind of code base (my modern ESM one, and the old CJS) just because of HH, was a real pain, and was getting more and more painful by the day. Thank you again for making my life easier and my work more productive. PS: sorry for the TS people for whom it doesn't work as easily. IMHO TS is a waste of time and clutters your code too much. (no troll intended, just my personal opinion). |
@fvictorio I'm so sorry for the late reply. This is the package.json dependency where hardhat is: "dependencies": { |
@cnohara I t hink the problem is that npm 7+ has a new behavior when using peer dependencies that, while super useful most of the time, makes it a pain in the ass when using pre-releases. There isn't much we can do about that afaik. |
Closing this in favor of #3043 for bookkeeping reasons. |
considering the function name
importCsjOrEsModule
, i suppose it should work with es modulesThe text was updated successfully, but these errors were encountered: