superjson icon indicating copy to clipboard operation
superjson copied to clipboard

Feature request: add commonjs and esm exports

Open ericallam opened this issue 1 year ago • 9 comments

Currently superjson does not export commonjs, only ESM. It would be great to support both formats. Our package that we use superjson in supports both formats and having superjson be ESM only causes some issues. It's really easy these days to create a dual-package using something like tshy (which is what we use). If you are open to it, I'd be happy to create a PR.

ericallam avatar Sep 19 '24 12:09 ericallam

Ya, I feel like whoever made this change isn't living in reality. ESM-only simply doesn't work for any app of medium complexity.

hashwarp avatar Oct 03 '24 00:10 hashwarp

Did you guys find a workaround? @ericallam @Spudfellah

palfaro91 avatar Oct 21 '24 21:10 palfaro91

@palfaro91 unfortunately the only way at the moment is to use dynamic imports.

ericallam avatar Oct 25 '24 21:10 ericallam

Going back to ^1.13.3 is a temporary solution

harijoe avatar Nov 05 '24 15:11 harijoe

@palfaro91 unfortunately the only way at the moment is to use dynamic imports.

That kinda works, but it doesn't work once you're using jest

jacquesg avatar Feb 19 '25 20:02 jacquesg

Also happy to submit a PR to support both ESM and CommonJS.

jacquesg avatar Feb 19 '25 20:02 jacquesg

@jacquesg There's a PR open here: https://github.com/flightcontrolhq/superjson/pull/301

Tried a few different hacks but ended up downgrading to ^1.13.3 for now.

aMoniker avatar Feb 25 '25 00:02 aMoniker

Tried to use github:acheronfail/superjson#support-cjs, but this did not work. The ESM/CJS error was replaced with one complaining about missing type declarations. The approach that did work, however, was cloning the repo from the PR and building the support-cjs branch and then using the dist-cjs folder as the source for superjson (in my case, the dependency entry ended up being file:../local_superjson where local_superjson is a symlink to the dist-cjs folder of the superjson fork). This will just be my temporary solution until that PR is merged.

Edit: Should also note I had to add a package.json file to the folder. The file looks like this:

{
  "version": "2.2.1",
  "name": "superjson",
  "license": "MIT",
  "type": "commonjs",
  "types": "./index.d.ts",
  "main": "./index.cjs",
  "browser": "./index.cjs",
  "exports": {
    ".": {
      "types": "./index.d.ts",
      "browser": "./index.cjs",
      "import": "./index.cjs",
      "require": "./index.cjs",
      "default": "./index.cjs"
    }
  },
  "files": [
    "*.js",
    "*.cjs",
    "*.d.ts"
  ]
}

PlanetTeamSpeakk avatar Feb 27 '25 14:02 PlanetTeamSpeakk

Going back to ^1.13.3 is a temporary solution

This is the only solution that worked for me in nx monorepo.

irunika avatar May 14 '25 13:05 irunika