Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cjs export is broken when importHelpers is set to true in tsconfig on consumer side #53

Closed
himanshuchawla009 opened this issue Aug 5, 2024 · 9 comments · Fixed by #56
Assignees

Comments

@himanshuchawla009
Copy link

himanshuchawla009 commented Aug 5, 2024

  • 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(...)

@Nesopie
Copy link
Collaborator

Nesopie commented Aug 5, 2024

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"));

@arijoon
Copy link

arijoon commented Dec 3, 2024

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

@Nesopie
Copy link
Collaborator

Nesopie commented Dec 3, 2024

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

@arijoon
Copy link

arijoon commented Dec 3, 2024

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

@junderw
Copy link
Member

junderw commented Dec 4, 2024

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.

@arijoon
Copy link

arijoon commented Dec 13, 2024

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

@junderw
Copy link
Member

junderw commented Dec 15, 2024

@Nesopie Said he'll work on it.
re: microsoft/TypeScript#50466

@junderw junderw assigned junderw and Nesopie and unassigned junderw Dec 15, 2024
@arijoon
Copy link

arijoon commented Dec 15, 2024

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

@junderw junderw linked a pull request Dec 21, 2024 that will close this issue
@junderw
Copy link
Member

junderw commented Dec 21, 2024

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

@junderw junderw changed the title cjs export is broken cjs export is broken when importHelpers is set to true in tsconfig on consumer side Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants