TypeChain: Error: Couldn't find ethers-v5

I am getting this error after trying to generate typechain files out of my artifacts:

Error: Couldn't find ethers-v5. Tried loading: @typechain/ethers-v5, typechain-target-ethers-v5, C:\Users\oxman\Code\defi\keep3r\keep3r-v2\ethers-v5.
Perhaps you forgot to install @typechain/ethers-v5?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 23 (10 by maintainers)

Most upvoted comments

Adding "*": ["node_modules/*"] to tsconfig.json fixes it.

https://github.com/dividab/tsconfig-paths/issues/88

I am leaning towards changing the default output directory to typechain-types to avoid this issue in the future.

@krzkaczor Are you sure this is resolved? I am still seeing the same issue. After much trial and error, the only thing which fixed it for me was:

"nohoist": [
  "**/typechain",
  "**/@typechain/*"
]

I found it was essential to stop @typechain/hardhat getting hoisted.

Finally I found out that ethers-v5 was loading the typechain library in its index.js, but instead of receiving the package when doing require('typechain'), it was getting my typechain generated types.

Interestingly that issue is still happening even with this set in paths:

"@typechain/*": ["node_modules/@typechain/*", "typechain/*"]

I thought that this would force tsconfig-paths to look for the @typechain/ethers-v5 package in node_modules, but it didn’t do that.

The only solution that worked was "*": ["node_modules/*"].

Update: Yeeaah… doing that made TypeChain work but broke my tests, which I run with Hardhat:

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/paulrberg/workspace/projects/prb-proxy/node_modules/nanoid/non-secure/index.js from /Users/paulrberg/workspace/projects/prb-proxy/node_modules/mocha/lib/utils.js not supported.
Instead change the require of index.js in /Users/paulrberg/workspace/projects/prb-proxy/node_modules/mocha/lib/utils.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/paulrberg/workspace/projects/prb-proxy/node_modules/mocha/lib/utils.js:12:18)
    at Object.<anonymous> (/Users/paulrberg/workspace/projects/prb-proxy/node_modules/mocha/lib/reporters/base.js:11:13)
    at Object.<anonymous> (/Users/paulrberg/workspace/projects/prb-proxy/node_modules/mocha/lib/reporters/index.js:5:31)
    at Object.<anonymous> (/Users/paulrberg/workspace/projects/prb-proxy/node_modules/mocha/lib/mocha.js:11:24)
    at Object.<anonymous> (/Users/paulrberg/workspace/projects/prb-proxy/node_modules/mocha/index.js:3:18)
    at /Users/paulrberg/workspace/projects/prb-proxy/node_modules/hardhat/builtin-tasks/test.js:52:80
    at async SimpleTaskDefinition.action (/Users/paulrberg/workspace/projects/prb-proxy/node_modules/hardhat/builtin-tasks/test.js:52:32)
    at async Environment._runTaskDefinition (/Users/paulrberg/workspace/projects/prb-proxy/node_modules/hardhat/internal/core/runtime-environment.js:135:20) {
  code: 'ERR_REQUIRE_ESM'
}

I think I will opt out of using paths for the time being.

@kiwi745 Great catch! Exactly my problem. You saved me a lot of debugging. They should update the docs with this information.

@krzkaczor Are you sure this is resolved? I am still seeing the same issue. After much trial and error, the only thing which fixed it for me was:

"nohoist": [
  "**/typechain",
  "**/@typechain/*"
]

I found it was essential to stop @typechain/hardhat getting hoisted.

Should I add this folder (typechain-types) to the .gitignore file?

@jchayan can u dump your package.json here? make sure that all versions are up-to-date. If this still doesn’t work and u use waffle uninstall it tmp.