1

I've created an hardhat project that uses typescript. As I'm trying to run the test from intellij I'm getting:

TSError: ⨯ Unable to compile TypeScript:
test/index.ts(2,10): error TS2305: Module '"hardhat"' has no exported member 'ethers'.

If I run the test from command line it works just fine:

enter image description here

Here is my intellij run configuration:

enter image description here

On a side note - everything works just fine with a javascript hardhat project (I'm able to debug it using intellij without any problem), the problem occurs only with a typescript hardhat project.

I have tried installing mocha-typescript and @testdeck/mocha and change the intellij run configuration to run using mocha-typescript:

enter image description here

But then got:

× ERROR: Error: Could not load ui "mocha-typescript":

 Error: Cannot find module 'C:\Users\Yogev\dev\temp\mocha-typescript'
Require stack:
- C:\NodeNpm\npm\node_modules\mocha\lib\cli\run-helpers.js
- C:\NodeNpm\npm\node_modules\mocha\lib\cli\options.js
- C:\NodeNpm\npm\node_modules\mocha\bin\mocha
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at exports.validateLegacyPlugin (C:\NodeNpm\npm\node_modules\mocha\lib\cli\run-helpers.js:234:27)
    at C:\NodeNpm\npm\node_modules\mocha\lib\cli\run.js:355:9 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\NodeNpm\\npm\\node_modules\\mocha\\lib\\cli\\run-helpers.js',
    'C:\\NodeNpm\\npm\\node_modules\\mocha\\lib\\cli\\options.js',
    'C:\\NodeNpm\\npm\\node_modules\\mocha\\bin\\mocha'
  ]

Why is that? How can I debug my tests using intellij in a typescript project?

BTW, this is the test file:

enter image description here

3 Answers 3

1

ts-node doesn't parse all files in the project directory for types which–while reduces startup time–could lead to missing types.

To override this, try adding either:

  • the --files flag to your ts-node arguments, or
  • set the TS_NODE_FILES environment variable to true.

This behaviour is also described in the official Hardhat documentation: https://hardhat.org/guides/typescript.html#running-your-tests-and-scripts-directly-with-ts-node

In your case setting the environment variable on the IntelliJ run configuration should resolve the problem.

0

I found that for me the problem with debugging Hardhat type-script tests was solved in the following steps:

  1. npm -i -D ts-mocha
  2. Creating a file in the project root directory: .mocharc.yaml. This article explains why it's needed: https://youtrack.jetbrains.com/issue/WEB-48850#focus=Comments-27-4615521.0-0
  3. Restart Intellij
  4. Run the tests from IntelliJ (click in the test on GUI and select run or debug)

Intellij will automatically create the configuration as shown below. Note that Extra Mocha Options are empty. It must be empty. If it's not empty then you created .mocharc.yaml in the wrong place.

enter image description here

0

I was able to solve Module '"hardhat"' has no exported member 'ethers' error by adding these 2 lines to tsconfig.json:

  "include": ["./scripts", "./test", "./typechain-types"],
  "files": ["./hardhat.config.ts"]

I also needed to add .mocharc.yaml (with some contents), but that was for fixing another error. More details here: https://github.com/KartanHQ/intellij-hardhat/issues/196

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.