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

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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