ethers-io / ethers.js

Complete Ethereum library and wallet implementation in JavaScript.
https://ethers.org/
MIT License
7.97k stars 1.85k forks source link

`ContractRunner` not exported properly for the browser (lib.esm) #3967

Open daramir opened 1 year ago

daramir commented 1 year ago

Ethers Version

6.2.0

Search Terms

esm, ContractRunner

Describe the Problem

Generating types using typechain (with --target=@typechain/ethers-v6) works without issue and can integrate the generated types with Typescript files that use ethers (v6). However, when building the DApp (or running vite dev), the compiled site will fail, because the ContractRunner cannot be found.

My surface level analysis tells me this could be due to the esm build exporting nothing to https://github.com/ethers-io/ethers.js/blob/main/lib.esm/providers/contracts.js, when in fact https://github.com/ethers-io/ethers.js/blob/main/src.ts/providers/contracts.ts is not empty. The commonjs build counterpart https://github.com/ethers-io/ethers.js/blob/main/lib.commonjs/providers/contracts.d.ts isn't empty either and exports the expected types/interface.

The error is a bit more evident when ethers is excluded from dependency optimization in vite config (see code snippet), after which the error turns into the error labeled as "Error when dependency is not optimized". Following the breadcrumbs the built files "/node_modules/ethers/lib.esm/index.js?v=6358794d" and "/node_modules/ethers/lib.esm/providers/index.js?v=6358794d" do not export ContractRunner, possibly due to the findings aforementioned.

Code Snippet

vite.config.ts

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [sveltekit()],

  optimizeDeps: {
    exclude: ['ethers'],
  },
});

Contract ABI

No response

Errors

Original error:

SyntaxError: The requested module '/node_modules/.vite/deps/ethers.js?v=a88f642f' does not provide an export named 'ContractRunner' (at Token__factory.ts:5:31)
handleError @   app.js:52
handle_error    @   client.js?v=1064d8a8:1331
_hydrate    @   client.js?v=1064d8a8:1781
await in _hydrate (async)       
start   @   start.js:22
(anonymous) @   (index):1908
Promise.then (async)        
(anonymous) @   (index):1907

Error when dependency is not optimized:

SyntaxError: The requested module '/node_modules/ethers/lib.esm/index.js?v=6358794d' does not provide an export named 'ContractRunner'


### Environment

Browser (Chrome, Safari, etc)

### Environment (Other)

@sveltejs/kit:1.12.0, vite:4.2.0
imaksp commented 1 year ago

Hi, https://github.com/ethers-io/ethers.js/blob/main/lib.esm/providers/contracts.js file cannot export interface as it is not supported by js file, it is like type only export so won't be present in .js file. it may be some other config related issue.

ricmoo commented 1 year ago

The ContractRunner is exported from the root. Maybe it's searching somewhere else?

daramir commented 1 year ago

The ContractRunner is exported from the root.

@ricmoo can you please to the code in this repo where that happens? i.e. the file you are referring to as "the root"?

daramir commented 1 year ago

I think this might be fixed by a47cad16a85f1ef8268542b3c6955c462a56a9d3, but haven't had the change to test and reproduce the issue on the latest version. Thinking of closing this issue, ideally with an answer of whether it's "completed" or "not planned". Will add to my backlog.