ethers.js: Cannot find module 'ethers/providers' or its corresponding type declarations.
Ethers Beta Version
6.0.0-beta-exports.15
Describe the Problem
Hi,
I tried to use
import { EventFilter, LogParams, Network } from 'ethers/providers'
since they are not available in ethers
module. But typescript failed to compile because can’t find type declaration
My environment is node 16, yarn 1.22 type script 4.9.4, my tsconfig.json looks like this:
{
"compilerOptions": {
"declaration": true,
"alwaysStrict": true,
"sourceMap": true,
"target": "esnext",
"lib": ["ES2021"],
"esModuleInterop": true,
"noImplicitReturns": true,
"noImplicitAny": true,
"resolveJsonModule": true,
"module": "commonjs",
"moduleResolution": "node",
"isolatedModules": true,
"strictNullChecks": true,
"stripInternal": true,
"noFallthroughCasesInSwitch": true,
"noEmitOnError": true,
// by pass issue: Could not find a declaration file for module 'buffer-layout' for now.
"skipLibCheck": true
}
}
Code Snippet
No response
Errors
No response
Environment
node.js
Environment (Other)
No response
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 1
- Comments: 15 (7 by maintainers)
Commits related to this issue
- Fixed commonjs imports missing types (#3703). — committed to ethers-io/ethers.js by ricmoo a year ago
- Fixed exports field order (#3703, #3755). — committed to ethers-io/ethers.js by ricmoo a year ago
- Re-adding definition files to require exports (#3703). — committed to ethers-io/ethers.js by ricmoo a year ago
- Fixed commonjs imports missing types (#3703). — committed to Woodpile37/ethers.js by ricmoo a year ago
- Fixed exports field order (#3703, #3755). — committed to Woodpile37/ethers.js by ricmoo a year ago
- Re-adding definition files to require exports (#3703). — committed to Woodpile37/ethers.js by ricmoo a year ago
- Fixed commonjs imports missing types (#3703). — committed to Woodpile37/ethers.js by ricmoo a year ago
- Fixed exports field order (#3703, #3755). — committed to Woodpile37/ethers.js by ricmoo a year ago
- Re-adding definition files to require exports (#3703). — committed to Woodpile37/ethers.js by ricmoo a year ago
The
ethers/lib/utils
was in v5. For v6 you can import fromethers/utils
or just feel free to import fromethers
since the utils are also exported from the top package.The problem with
.mjs
files is they break importing, since esm requires extensions when importing and TypeScript doesn’t re-write them. So, in that case the ESM files would need to beimport { foo } from "./bar.mjs"
and common JS would needconst { foo } = require("./bar.cjs")
. I’ve learned my lesson from v5 about post-processing the output, which breaks standard tools.The east solution is just to re-include the
.d.ts
files in the/lib.commonjs
folder; it is a bit messy, but until TypeScript can properly resolve the type definitions, it works. 😃I fixed this by just adding declaration generation to the
tsconfig.commonjs.json
.Thanks for your research! 😃