cardano-serialization-lib icon indicating copy to clipboard operation
cardano-serialization-lib copied to clipboard

Issues with @emurgo/cardano-serialization-lib-nodejs

Open face0612 opened this issue 4 years ago • 14 comments

Using webpack 5.24.4

  1. Error: Cannot find module 'util' is thrown when loading. Commenting out const { TextDecoder } = require(String.raw); fixes the issue, but is there a way to achieve this without modifying the code myself?

  2. path set on const path = require('path').join(__dirname, 'cardano_serialization_lib_bg.wasm'); yields incorrect path as this does not lead to node_modules directory

  3. WebAssembly.Compile is disallowed on the main thread, if the buffer size is larger than 4KB. Use WebAssembly.compile, or compile on a worker thread is thrown. This is fixed only when I replace

const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
wasm = wasmInstance.exports;
module.exports.__wasm = wasm;

with

WebAssembly.compile(bytes).then((
    mod => {
        WebAssembly.instantiate(mod, imports).then(
            wasmInstance => {
                wasm = wasmInstance.exports;
                module.exports.__wasm = wasm;        
            }
        );
    }
));

face0612 avatar May 22 '21 14:05 face0612

  1. You may need a polyfill for text-decoder. This is a common issue with WASM on older platforms
  2. Haven't heard of this before. Probably it's caused by a different issue
  3. Historically WASM modules have to be imported asynchronously. Webpack 5 added support for synchronous loading of WASM if you set an experimental flag for it though. Typescript also has better support for WASM than Babel I think.

SebastienGllmt avatar May 22 '21 21:05 SebastienGllmt

Having a similar issue with Webpack 5. Is there an example of how to make this work? A similar issue can be found here for another package:

https://github.com/apollographql/federation/issues/255

Any help appreciated.

electic avatar May 23 '21 22:05 electic

I updated Yoroi to Webpack 5 a few months ago and didn't have any issues at the time. Maybe this PR is useful to you: https://github.com/Emurgo/yoroi-frontend/pull/1929

SebastienGllmt avatar May 24 '21 00:05 SebastienGllmt

Took a look at this, I think the difference here is your use babel-node when calling different npm run functions and we just use webpack. The result is that babel-loader does not polyfill node_modules because it is ignored in most configs.

Here is our package.json file:

"scripts": {
    "build-main": "cross-env NODE_ENV=production webpack --config webpack.main.prod.config.js",
    "build-renderer": "cross-env NODE_ENV=production webpack --config webpack.renderer.prod.config.js",
    "build": "npm run build-main && npm run build-renderer",
    "start-renderer-dev": "webpack serve --config webpack.renderer.dev.config.js",
    "start-main-dev": "webpack --config webpack.main.config.js && electron ./dist/main.js",
    "start-dev": "cross-env START_HOT=1 ELECTRON_IS_DEV=1 npm run start-renderer-dev",
    "start-prod": "cross-env START_HOT=1 ELECTRON_IS_DEV=0 npm run start",
...

Here is our dev config for webpack:

{
                test: /\.js$/,
                exclude: /node_modules\/(?!(bech32|ripple-lib))/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env'],
                        plugins: ["@babel/plugin-syntax-dynamic-import"]
                    }
                }
            },

I think this is the issue. Also, Polkadot folks seem to use wasm with no issue at least that I can see.

https://www.npmjs.com/package/@polkadot/wasm-crypto

I took a look at this and would be curious to see what the difference is in packaging is.

electic avatar May 25 '21 21:05 electic

Every WASM project has at least two different builds:

  • NodeJS build
  • Browser build

If you're trying to connect from Webpack, you need to use the browser build, but it sounds like you may be trying to use the nodejs build. Can you make sure you're using https://www.npmjs.com/package/@emurgo/cardano-serialization-lib-browser when building for a web environment?

SebastienGllmt avatar May 26 '21 01:05 SebastienGllmt

hi hi. Actually we use webpack to package an electron app. The module actually sits in the main node.js process not the render process. Webpack just happens to package the whole thing up.

electic avatar May 26 '21 02:05 electic

The new 7.1.0 release includes a fix for Webpack 5 builds that may help you if you're still having issues

SebastienGllmt avatar Jun 20 '21 01:06 SebastienGllmt

Any solution to "Cannot find module 'util'" ? I've tried to add a polyfill and nothing.

edwint88 avatar Nov 10 '21 01:11 edwint88

Not sure if anyone here is using Nextjs, but I'm having the same issue where it can't find the util module. When I comment out the line, or add a polyfill, I get a new error

Error: ENOENT: no such file or directory, open '/Users/blakebrown/Documents/Code/wagmi/.next/server/cardano_serialization_lib_bg.wasm

Has anyone been able to successfully load this library in Next?

BlakeBrown avatar Nov 11 '21 03:11 BlakeBrown

Hi,

I've been hit with this same error (as in issue 1 in the first post) while trying to bundle my app: image

I'm using "@emurgo/cardano-serialization-lib-nodejs": "^11.2.1". Is there any way to resolve this, and actually use the imported modules from the package?

I am trying to initiate a transaction from a connected wallet in the cardano browser extension

Please help.

amnambiar avatar Jan 17 '23 17:01 amnambiar

@amnambiar the error is telling you what to do. You have to specify the resolve.fallback fields inside your webpack config. If you're using a build system that is hiding the webpack config from you, you may have to "eject" to modify it

Also note you're using the nodejs bindings. If you're using a browser extension, you may be interested in the browser bindings instead (separate NPM package)

SebastienGllmt avatar Jan 17 '23 19:01 SebastienGllmt

@SebastienGllmt A bit of a context - I'm working on a react web application, and trying to interact with cardano wallets from JS. I've created the app using create-react-app; and the app bundles using npm.

Do I still need to use browser bindings?

amnambiar avatar Jan 18 '23 13:01 amnambiar

@amnambiar yeah, most likely you want the browser bindings if you're not depending on any nodejs scripts for your build process. This library is more low-level, so if you want a more high-level library for interacting with Cardano I suggest using Lucid

SebastienGllmt avatar Jan 18 '23 14:01 SebastienGllmt

Thanks @SebastienGllmt , Replaced @emurgo/cardano-serialization-lib-nodejs with @emurgo/cardano-serialization-lib-asmjs and this was resolved!

amnambiar avatar Jan 19 '23 12:01 amnambiar