entropyxyz / sdk

Official JavaScript SDK for Entropy blockchain.
GNU Affero General Public License v3.0
7 stars 0 forks source link

`entropy-js` throws client side #236

Closed benschac closed 6 months ago

benschac commented 10 months ago

After importing and console.log'ing the entropy instance, I'm getting this error:

Module not found: Can't resolve 'fs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../node_modules/@entropyxyz/entropy-js/dist/index.js
../../packages/app/features/home/screen.tsx
./pages/index.tsx
<w> [webpack.cache.PackFileCacheStrategy] Restoring pack failed from /Users/benjaminschachter/entropy-client/apps/next/.next/cache/webpack/client-development-fallback.pack.gz: Error: incorrect data check
 ⨯ ../../node_modules/@entropyxyz/x25519-chacha20poly1305-nodejs/x25519-chacha20poly1305-nodejs.js:463:0
Module not found: Can't resolve 'fs'

As a third party dev, I'd assume either:

An option would be to polyfill fs but the package is already kind big #232

PR with to reproduce: https://github.com/entropyxyz/entropy-client/pull/112

local env:

  System:
    OS: macOS 14.0
    CPU: (12) arm64 Apple M2 Max
    Memory: 244.77 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.asdf/installs/nodejs/18.16.0/bin/node
    Yarn: 4.0.1 - ~/.asdf/installs/nodejs/18.16.0/bin/yarn
    npm: 9.5.1 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 8.8.0 - ~/.asdf/installs/nodejs/18.16.0/bin/pnpm
    bun: 1.0.4 - ~/.bun/bin/bun
  Browsers:
    Chrome: 119.0.6045.105
    Safari: 17.0
benschac commented 10 months ago

Additionally, also probably worth testing on other popular build envs like

frankiebee commented 9 months ago

can you double check if window is undefined?

benschac commented 9 months ago

CleanShot 2023-11-30 at 13 53 31@2x window is here.

The issue is that entropy-js has a requirement on a nodejs module fs that is not available in the client.

I had to polyfill it here: https://github.com/entropyxyz/entropy-client/commit/c6618c07a6bd55d7edc923350c5c66f18018693e#diff-0f913df15d01006c7733a9e4969f6335577a8a1ca3e1669c0013f57361c9ba7bR56

which, both adds more to the bundle, and is an additional step for someone building on the browser, that should be avoided.

https://developer.mozilla.org/en-US/docs/Web/API/File - which can handle fs functionality for a client sdk.