Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module 'ethers/providers' or its corresponding type declarations. #3703

Closed
zfy0701 opened this issue Jan 31, 2023 · 16 comments
Closed
Assignees
Labels
fixed/complete This Bug is fixed or Enhancement is complete and published. v6 Issues regarding v6

Comments

@zfy0701
Copy link

zfy0701 commented Jan 31, 2023

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

@zfy0701 zfy0701 added the v6 Issues regarding v6 label Jan 31, 2023
@zfy0701
Copy link
Author

zfy0701 commented Jan 31, 2023

If I change to moduelResolution to node16, error changes

error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("ethers")' call instead.

@ricmoo ricmoo added the on-deck This Enhancement or Bug is currently being worked on. label Jan 31, 2023
@ricmoo
Copy link
Member

ricmoo commented Jan 31, 2023

I think you would want "node16" to use the exports, but surprized it won’t resolve to the require paths for you. Looking into this…

@zfy0701
Copy link
Author

zfy0701 commented Jan 31, 2023

you can use this example to debug if needed

https://github.com/zfy0701/sample-ethers6

@zfy0701
Copy link
Author

zfy0701 commented Jan 31, 2023

"cp -Rn node_modules/ethers/types/* node_modules/ethers/lib.commonjs"

this works for me in conjunction with use node16, seems node16 with commonjs doesn't respect types and need .d files sibling to the ts files

@ricmoo
Copy link
Member

ricmoo commented Jan 31, 2023

I might try this format:

// package.json
{
    "name": "my-package",
    "type": "module",
    "exports": {
        ".": {
            // Entry-point for `import "my-package"` in ESM
            "import": {
                // Where TypeScript will look.
                "types": "./types/esm/index.d.ts",
                // Where Node.js will look.
                "default": "./esm/index.js"
            },
            // Entry-point for `require("my-package") in CJS
            "require": {
                // Where TypeScript will look.
                "types": "./types/commonjs/index.d.cts",
                // Where Node.js will look.
                "default": "./commonjs/index.cjs"
            },
        }
    },
    // Fall-back for older versions of TypeScript
    "types": "./types/index.d.ts",
    // CJS fall-back for older versions of Node.js
    "main": "./commonjs/index.cjs"
}

@ricmoo
Copy link
Member

ricmoo commented Jan 31, 2023

Quick question; what version of TypeScript are you using?

@zfy0701
Copy link
Author

zfy0701 commented Jan 31, 2023

Quick question; what version of TypeScript are you using?

4.9.4

@ricmoo
Copy link
Member

ricmoo commented Feb 3, 2023

I fixed this by just adding declaration generation to the tsconfig.commonjs.json.

Thanks for your research! :)

@ricmoo ricmoo closed this as completed Feb 3, 2023
@ricmoo ricmoo added fixed/complete This Bug is fixed or Enhancement is complete and published. and removed on-deck This Enhancement or Bug is currently being worked on. labels Feb 3, 2023
@songkeys
Copy link

songkeys commented Feb 23, 2023

I get the same error using the same config file in v6.0.7.

import { ethers } from 'ethers'
                        ~~~~~~
module "......../node_modules/ethers/types/index"
The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("ethers")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '........./package.json'.

typescript@4.9.5


My original tsconfig.json works fine in v5 but not in v6. I'll get the error above:

{
  "compilerOptions": {
    "target": "es2020",
    "module": "esnext",
    "strict": true,
    "esModuleInterop": true,
    "moduleResolution": "node16",
    "skipLibCheck": true,
    "noUnusedLocals": true,
    "noImplicitAny": true,
    "allowJs": true,
    "resolveJsonModule": true,
    "experimentalDecorators": true
  },
  "exclude": ["dist"]
}

I think we should drop "type": "module" in package.json? see: gvergnaud/ts-pattern#110 (comment)

@ricmoo
Copy link
Member

ricmoo commented Feb 23, 2023

The type must be module to work in ESM projects. I thought fixing the order of exports would fix this, but I will re-add the type declarations to the 'lib.commonjs`.

@songkeys
Copy link

The type must be module to work in ESM projects.

Alternatively, output the dist js file with ext .mjs. I think this would be much easier.

@ricmoo
Copy link
Member

ricmoo commented Feb 24, 2023

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. :)

@ricmoo
Copy link
Member

ricmoo commented Mar 7, 2023

I've re-added the types, since re-ordering the exports didn't seem to resolve the issue.

@hibataha
Copy link

hibataha commented Aug 28, 2023

Error: Cannot find module 'ethers/lib/utils'

Still getting the above error in next js app structure and in an nx repo

Do anyone by any chance have a solution ?

@zemse
Copy link
Collaborator

zemse commented Aug 29, 2023

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.

Woodpile37 pushed a commit to Woodpile37/ethers.js that referenced this issue Jan 14, 2024
Woodpile37 pushed a commit to Woodpile37/ethers.js that referenced this issue Jan 14, 2024
Woodpile37 pushed a commit to Woodpile37/ethers.js that referenced this issue Jan 14, 2024
@Jeremiegmoore
Copy link

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

node_modules/ethers/types/index

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed/complete This Bug is fixed or Enhancement is complete and published. v6 Issues regarding v6
Projects
None yet
Development

No branches or pull requests

6 participants