Skip to content

ESM Support #1372

@joacohoyos

Description

@joacohoyos

Summary

Hi Everoyne!

Creating this issue cause at jpg.store we were trying to use Blaze (which internally uses cardano-js-sdk)and at the same time using the sdk directly for a lot of datum building and other cardano relate stuff and noticed several issues while using the library in ESM mode.
Talking with people at butane, they had issues with ESM too, so it seems its not an problem only for us.

@pocesar ( our js wizard) did an analysis of the codebase and even try fixing it with no luck. Here there is a rough summary of the findings he got and some output he gave us.

  • If you try to use this library in a "type": "module" (ESM) codebase it will fail because the generated libraries in dist/esm folder aren't valid ESM modules.
  • We tried to fix the ESM build step, but it's nearly impossible with code changes, since some files are mixing CJS and ESM dependencies, using import { } from 'dep' when it's a CJS and vice-versa. ESM modules doesn't have a require global function like CJS (the same goes to all CJS specific globals, like require.resolve, __dirname, __filename).
  • Some dependencies got a mismatching .d.ts types for ESM and CJS, that makes the build break as well.
  • Package manager vendor specific hacks, like workspace:~ makes it even harder to make changes.

@pocesar and also @micahkendall and @EzePze from butane recommended actually using a build step through something like esbuild, rollup, tsup instead of relying on correctness from the tsc compiler.

Think this is a major issue to try to address as ESM is starting to get more and more adoption and CJS is starting to almost be unproductive to create new codebases with given a lot of the newer libraries are starting to ship with esm support only.

Steps to reproduce the bug

https://github.com/joacohoyos/cardano-sdk-esm-issue

Here there is a repo which you already start getting issue with if you try to run it.

It start with libsodium issues, but even if you fix those you then start getting different issues with different pieces of the library.

According to @pocesar another quick way to see the issue is doing a --target ESNext --module ESNext --moduleResolution Bundler inside each workspace module.

Actual Result

Getting multiple issues when using with ESM

Expected Result

Library works both with CJS and ESM environemntes

SDK version

^0.35.2

Environment type

  • Node.js
  • Browser
  • Browser Extension
  • Other

Environment details

Node: 20.9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions