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

error when using es modules [ERR_REQUIRE_ESM] #957

Closed
cruzdanilo opened this issue Oct 26, 2020 · 50 comments
Closed

error when using es modules [ERR_REQUIRE_ESM] #957

cruzdanilo opened this issue Oct 26, 2020 · 50 comments

Comments

@cruzdanilo
Copy link

cruzdanilo commented Oct 26, 2020

considering the function name importCsjOrEsModule, i suppose it should work with es modules

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: xxx/hardhat.config.js
require() of ES modules is not supported.
require() of xxx/hardhat.config.js from xxx/node_modules/hardhat/internal/core/config/config-loading.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename hardhat.config.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from xxx/package.json.

    at new NodeError (node:internal/errors:258:15)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1100:13)
    at Module.load (node:internal/modules/cjs/loader:948:32)
    at Function.Module._load (node:internal/modules/cjs/loader:789:14)
    at Module.require (node:internal/modules/cjs/loader:972:19)
    at require (node:internal/modules/cjs/helpers:88:18)
    at importCsjOrEsModule (xxx/node_modules/hardhat/src/internal/core/config/config-loading.ts:18:20)
    at Object.loadConfigAndTasks (xxx/node_modules/hardhat/src/internal/core/config/config-loading.ts:61:18)
    at main (xxx/node_modules/hardhat/src/internal/cli/cli.ts:116:20) {
  code: 'ERR_REQUIRE_ESM'
}

hardhat@2.0.1
node@15.0.1
@cruzdanilo
Copy link
Author

i tried renaming the config file to hardhat.config.cjs and using --config hardhat.config.cjs, but i got a similar error from mocha (which supports esm)

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: xxx/test/contract.test.js
require() of ES modules is not supported.
require() of xxx/test/contract.test.js from xxx/node_modules/mocha/lib/mocha.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename contract.test.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from xxx/package.json.

    at new NodeError (node:internal/errors:258:15)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1100:13)
    at Module.load (node:internal/modules/cjs/loader:948:32)
    at Function.Module._load (node:internal/modules/cjs/loader:789:14)
    at Module.require (node:internal/modules/cjs/loader:972:19)
    at require (node:internal/modules/cjs/helpers:88:18)
    at xxx/node_modules/mocha/lib/mocha.js:349:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (xxx/node_modules/mocha/lib/mocha.js:346:14)
    at Mocha.run (xxx/node_modules/mocha/lib/mocha.js:1006:10) {
  code: 'ERR_REQUIRE_ESM'
}

@cruzdanilo cruzdanilo changed the title error when using esm config file error when using es modules [ERR_REQUIRE_ESM] Oct 26, 2020
@alcuadrado
Copy link
Member

@cruzdanilo can you provide instructions about how to reproduce this?

@cruzdanilo
Copy link
Author

just add "type": "module" to package.json. the error will pop even with an empty hardhat.config.js.

@numismatic4
Copy link

I have the same problem. How did Uniswap solve this, they are using es6 modules

@fvictorio
Copy link
Member

@numismatic4 I think Uniswap uses typescript, not es6 modules.

@David-Kneel
Copy link

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 hardhat test, the result is:

❯ 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 hardhat's internal mocha is not picking up .mocharc.js switches as defined below:

module.exports = {
    timeout: 20000,
    require: ["esm"],
    recursive: true,
    exit: true
}

@alcuadrado
Copy link
Member

.mocharc.js is not used when running the hardhat cli. You can try node --require esm node_modules/.bin/hardhat, or running mocha directly.

@David-Kneel
Copy link

.mocharc.js is not used when running the hardhat cli. You can try node --require esm node_modules/.bin/hardhat, or running mocha directly.

It is now working as it should. I followed your suggestion and added "test": "node --require esm node_modules/.bin/hardhat test", to package.json. Now yarn test runs successfully:

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)

@nhasanli
Copy link

nhasanli commented Sep 9, 2021

I'm having a similar problem..

I'm using typescript and my package.json has "type": "module".

I try compiling with npx hardhat compile and get an error back when node tries to require(filePath) at "/node_modules/hardhat/src/internal/core/config/config-loading.ts:23:20)"

function importCsjOrEsModule(filePath: string): any {
  const imported = require(filePath);
  return imported.default !== undefined ? imported.default : imported;
}

Full error trace

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/nhh/Documents/GitHub/video-marketplace/hardhat.config.ts
require() of ES modules is not supported.
require() of /Users/nhh/Documents/GitHub/video-marketplace/hardhat.config.ts from /Users/nhh/Documents/GitHub/video-marketplace/node_modules/hardhat/internal/core/config/config-loading.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /Users/nhh/Documents/GitHub/video-marketplace/package.json.

    at createErrRequireEsm (/Users/nhh/Documents/GitHub/video-marketplace/node_modules/ts-node/dist-raw/node-cjs-loader-utils.js:97:15)
    at assertScriptCanLoadAsCJSImpl (/Users/nhh/Documents/GitHub/video-marketplace/node_modules/ts-node/dist-raw/node-cjs-loader-utils.js:30:11)
    at Object.require.extensions.<computed> [as .ts] (/Users/nhh/Documents/GitHub/video-marketplace/node_modules/ts-node/src/index.ts:1302:5)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
    at importCsjOrEsModule (/Users/nhh/Documents/GitHub/video-marketplace/node_modules/hardhat/src/internal/core/config/config-loading.ts:23:20)
    at Object.loadConfigAndTasks (/Users/nhh/Documents/GitHub/video-marketplace/node_modules/hardhat/src/internal/core/config/config-loading.ts:66:18)
    at main (/Users/nhh/Documents/GitHub/video-marketplace/node_modules/hardhat/src/internal/cli/cli.ts:126:20) {
  code: 'ERR_REQUIRE_ESM'
}

My hardhat.config.ts in same folder as package.json and tsconfig.json

import dotenv from "dotenv";
dotenv.config();
import "@nomiclabs/hardhat-etherscan";
import "@nomiclabs/hardhat-waffle";
import "hardhat-gas-reporter";
import "solidity-coverage";
import { task } from "hardhat/config";
import { HardhatUserConfig } from "hardhat/types";

task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
  const accounts = await hre.ethers.getSigners();

  for (const account of accounts) {
    console.log(await account.address);
  }
});

const config: HardhatUserConfig = {
  solidity: "0.8.4",
  networks: {
    hardhat: {
      initialBaseFeePerGas: 0, 
    },
    ropsten: {
      url: `https://ropsten.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
      accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : undefined,
    },
    rinkeby: {
      url: `https://rinkeby.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
      accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : undefined,
    },
  },
  gasReporter: {
    enabled: process.env.REPORT_GAS !== undefined,
    currency: "USD",
  },
  etherscan: {
    apiKey: process.env.ETHERSCAN_API_KEY,
  },
};

export default config;

@fvictorio
Copy link
Member

I'm using typescript and my package.json has "type": "module".

AFAIK we don't support that yet. @alcuadrado can you confirm?

@Olyno
Copy link

Olyno commented Sep 19, 2021

I tried to use hardhat for the first time, and got this issue when using it with "type": "module"

Edit: Looks like you're using the require when compiling with esm (e.g here or here). You're using Typescript. Please use the import esm from Typescript and then compile the project in cjs + esm. Configure correctly the package.json file and it gonna work fine:

"main": "./dist/cjs/mypkg.js",
"module": "./dist/esm/mypkg.js",

@webhype
Copy link

webhype commented Oct 12, 2021

@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):

$ node -v
v14.18.1
$ yarn add esm
_...lots of stuff..._
$ node --require esm node_modules/.bin/hardhat test
/Users/macuser/MYPROJECT/node_modules/.bin/hardhat:11
const task_names_1 = require("../../builtin-tasks/task-names");
                     ^
/Users/macuser/MYPROJECT/node_modules/.bin/hardhat:1
Error: Cannot find module '../../builtin-tasks/task-names'
Require stack:
- /Users/macuser/MYPROJECT/node_modules/.bin/hardhat
    at Object.<anonymous> (/Users/macuser/MYPROJECT/node_modules/.bin/hardhat:11:22)
    at Generator.next (<anonymous>)

Without esm it looks like this:

$ hh
$ hh
An unexpected error occurred:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/macuser/MYPROJECT/hardhat.config.js
require() of ES modules is not supported.
require() of /Users/macuser/MYPROJECT/hardhat.config.js from /Users/macuser/MYPROJECT/node_modules/hardhat/internal/core/config/config-loading.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename hardhat.config.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/macuser/MYPROJECT/package.json.

    at new NodeError (internal/errors.js:322:7)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1102:13)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at importCsjOrEsModule (/Users/macuser/MYPROJECT/node_modules/hardhat/src/internal/core/config/config-loading.ts:23:20)
    at Object.loadConfigAndTasks (/Users/macuser/MYPROJECT/node_modules/hardhat/src/internal/core/config/config-loading.ts:66:18)
    at main (/Users/macuser/MYPROJECT/node_modules/hardhat/src/internal/cli/cli.ts:129:20) {
  code: 'ERR_REQUIRE_ESM'

Note: my hardhat.config.js is written in ESM syntax and starts with:

import { config as dotEnvConfig } from "dotenv";
dotEnvConfig();
import "@nomiclabs/hardhat-etherscan";
import "@nomiclabs/hardhat-waffle";
import "@nomiclabs/hardhat-ethers";
// and much more

@alcuadrado
Copy link
Member

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.

@aspiers
Copy link
Contributor

aspiers commented Nov 7, 2021

I've run into this too. It's pretty confusing, given that import works fine in hardhat.config.ts, and frustrating given how long ES modules have been around, but I'm certainly not complaining as we're all victims of the total mess which is the chaotic evolution of the JS ecosystem.

yarn add esm and node --require esm node_modules/.bin/hardhat test seem to do the trick for me too, although this seems pretty ugly. Thanks to all for sharing your suggestions!

@alcuadrado
Copy link
Member

Another alternative is just to use ts with a very permissive config. "strict": false should probably be enough.

@aspiers
Copy link
Contributor

aspiers commented Nov 8, 2021

"strict": false doesn't have any effect here :-(

@alcuadrado
Copy link
Member

Are you using .ts as an extension?

@aspiers
Copy link
Contributor

aspiers commented Nov 9, 2021

Yes.

@alcuadrado
Copy link
Member

There goes my theory :( I guess using esm is the less intrusive option then.

@Olyno
Copy link

Olyno commented Nov 10, 2021

I still got the problem on my side using esm, that's why i came here report the issue

@ddnexus
Copy link

ddnexus commented May 25, 2022

After so long, is it still not a priority? Typescript 4.7 is out and updating your typescript syntax to use import instead of require doesn't seem a lot of work. Am I missing something?

@aspiers
Copy link
Contributor

aspiers commented May 25, 2022

I'm also concerned that this is marked as priority:low. From what I've seen, the majority of the EVM space moved to TypeScript and ESM a long time ago, so Hardhat is in danger of getting left behind in this respect.

@Olyno
Copy link

Olyno commented May 25, 2022

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).

@fvictorio
Copy link
Member

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.

@Sceat
Copy link

Sceat commented Aug 16, 2022

Heavily following this, commonJs is a fossil of the past and should never be used. Web3 is leading the future so i would suggest to mark this issue as priority:high

@brozorec
Copy link

Hi everyone. We just released a version of Hardaht under the esm npm tag which includes basic support for ESM.

The way this works is that your Hardhat config still needs to be CommonJS, and so it has to be named hardhat.config.cjs (or .cts, if your project is in typescript). But your scripts and tests can be ESM now, or you can mix CJS and ESM for both tests and scripts.

Please give it a try (npm i hardhat@esm, or equivalent yarn command) and let us know if you find any issues!

hey this is awesome 👍 I'm about to try it out and changed hardhat.config.ts to hardhat.config.cts but it doesn't seem to detect the config. It promts this when I run hardhat test:
image

@Sceat
Copy link

Sceat commented Sep 26, 2022

with hardhat.config.cjs i can confirm that it works nicely

@brozorec
Copy link

Yes, it works with .cjs but not with .cts

@fvictorio
Copy link
Member

I'm about to try it out and changed hardhat.config.ts to hardhat.config.cts but it doesn't seem to detect the config. It promts this when I run hardhat test:

@brozorec sorrry about that! I published a new version that should fix that issue. npm i hardhat@esm should upgrade it to that version.

@fvictorio
Copy link
Member

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 .cts to work (which kind of defeats the purpose?) I'll let you know folks here when that's working reasonably well.

@Sceat
Copy link

Sceat commented Sep 28, 2022

actually what is the point of using esm if you use typescript, you already have access to the syntax

@fvictorio
Copy link
Member

@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.

@Olyno
Copy link

Olyno commented Sep 28, 2022

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 require used in functions to import modules dynamically.

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.

@iamnader
Copy link

iamnader commented Oct 4, 2022

does @nomicfoundation/hardhat-toolbox work with the esm version? Doesn't seem to for me

@fvictorio
Copy link
Member

@iamnader if you install the toolbox and then use the esm tag of hardhat (npm i hardhat@esm), it should work, yes. If it doesn't, please tell me what's failing.

@mihaic195
Copy link

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.

hardhat@esm had exactly the same result for me.

@fvictorio
Copy link
Member

@mihaic195 yes, adding support for typescript + ESM will be hard, because there's not good support for that in ts-node yet 😞

@David-Kneel
Copy link

I use hardhat and ESM without any problems. See https://github.com/TachyonicFinance/faster-than-light. The trick is calling node --require esm node_modules/.bin/hardhat instead of hardhat once the module esm is installed.

@mihaic195
Copy link

@David-Kneel it works because you're not using typescript

@David-Kneel
Copy link

@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

@David-Kneel
Copy link

@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.

@cnohara
Copy link

cnohara commented Oct 19, 2022

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?

@fvictorio
Copy link
Member

@cnohara does that happen after you install something?

@cnohara
Copy link

cnohara commented Oct 22, 2022

@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?

@fvictorio
Copy link
Member

@cnohara can you share the hardhat entry in your package.json's dependencies?

@slesolliec
Copy link

  1. installed hardhat@esm
  2. changed my hardhad.config.js filename to hardhat.config.cjs
  3. changed my ugly require() in my scripts to beautiful imports
  4. fixed a few glitches which are not related to hardhat, but to small differences between commonJS and ESM

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).

@cnohara
Copy link

cnohara commented Nov 10, 2022

@fvictorio I'm so sorry for the late reply. This is the package.json dependency where hardhat is:

"dependencies": {
"@graphql-tools/graphql-file-loader": "^7.5.9",
"@graphql-tools/load": "^7.8.4",
"@graphql-yoga/render-graphiql": "3.0.0-next.10",
"@prisma/client": "^4.6.0",
"@supabase/auth-helpers-sveltekit": "^0.8.6",
"@supabase/supabase-js": "^2.0.5",
"@urql/exchange-multipart-fetch": "^1.0.1",
"@urql/svelte": "^3.0.1",
"axios": "^1.1.3",
"codable": "^0.1.17",
"graphql": "^16.6.0",
"graphql-tools": "^8.3.10",
"graphql-yoga": "3.0.0-next.10",
"hardhat": "^2.12.0-esm.1",
"ipfs-http-client": "^59.0.0",
"jose": "^4.10.4"
}

@fvictorio
Copy link
Member

@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.

@fvictorio
Copy link
Member

Closing this in favor of #3043 for bookkeeping reasons.

@fvictorio fvictorio closed this as not planned Won't fix, can't repro, duplicate, stale Dec 30, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests