bs58 icon indicating copy to clipboard operation
bs58 copied to clipboard

cjs export is broken

Open himanshuchawla009 opened this issue 1 year ago • 1 comments

  • Importing this library and calling any of the function fails in node js.

For ex: Both code snippets fails with error: encode is not a function

import bs58 from "bs58";

bs58.encode(...)

import * as  bs58 from "bs58";

bs58.encode(...)

himanshuchawla009 avatar Aug 05 '24 06:08 himanshuchawla009

Import statements only work in esm modules (if you have "type": "module" in package.json). For CJS can you try this instead?

const bs58 = require("bs58").default;

bs58.encode(Buffer.from("test", "utf8"));

Nesopie avatar Aug 05 '24 23:08 Nesopie

in typescript the error persists if using cjs and require pattern is not desired

arijoon avatar Dec 03 '24 17:12 arijoon

@arijoon can you share minimal working reproduction? You can import CJS modules using ESM syntax

Nesopie avatar Dec 03 '24 17:12 Nesopie

Sure I'll make a repo later in the week with reproduction. Using "typescript": "5.6.3",, the following error persists when importing bs58 with import bs58 from 'bs58':

The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("bs58")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '/code/rhino-core/packages/dvf-bridge-api-spec/package.json'.

For now I've reverted to v5 which does not have this issue

arijoon avatar Dec 03 '24 17:12 arijoon

What is the NodeJS version?

Are you using external tools like bun or deno or something?

Even though bs58 is marked as an ESM, it contains the require exports for CJS inside package.json... so something in your build pipeline is so old that it can't comprehend the exports key of package.json...

We can look into this when you give us a small repository to try it out with.

junderw avatar Dec 04 '24 04:12 junderw

Repository to reproduce the issue: https://github.com/arijoon/bs58-53

arijoon avatar Dec 13 '24 20:12 arijoon

@Nesopie Said he'll work on it. re: https://github.com/microsoft/TypeScript/issues/50466

junderw avatar Dec 15 '24 01:12 junderw

Thanks, there's also a module-sync available with more recent versions of node, we're patching some of our deps to add this and it gets rid of the dual package issue at least on node https://nodejs.org/en/blog/release/v22.10.0

arijoon avatar Dec 15 '24 15:12 arijoon

@arijoon Please review #56 and see if it fixes the issue.

junderw avatar Dec 21 '24 05:12 junderw