ensdomains / ensjs

ENS javascript library for contract interaction
MIT License
123 stars 56 forks source link

vite: error when initializing ENSInstance (imported.GraphQLClient is not a constructor) #90

Closed appleseed-iii closed 1 year ago

appleseed-iii commented 1 year ago

in a vite application loading ensjs I get the following error when initializing a new ENSInstance:

TypeError: imported.GraphQLClient is not a constructor
    at GqlManager.setUrl (GqlManager.mjs:68:21)
    at async ENS.setProvider (index.mjs:127:5)

I'm running the following package versions: "@ensdomains/ensjs": "^3.0.0-alpha.39" "vite": "^3.1.8"

appleseed-iii commented 1 year ago

here's a minimal reproduction. Just run the app with yarn dev and then check the console.log

appleseed-iii commented 1 year ago

After doing a little digging I found a fix for this. I put the fix here: https://github.com/appleseed-iii/vite-ensjs-v3/pull/1/files

The root problem is that vite doesn't easily bundle CJS packages that are imported via ESM packages. In this case graphql-request (CJS) is imported via the ESM ens package.

Documentation from vite for the fix

image

Closing this since it's not ensjs related