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

Most upvoted comments

The ethers/lib/utils was in v5. For v6 you can import from ethers/utils or just feel free to import from ethers 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 be import { foo } from "./bar.mjs" and common JS would need const { 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! 😃