question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Poor CommonJS experience

See original GitHub issue

Describe the bug

Many projects don’t have the option of switching wholesale over to ESM, and must use CommonJS. This will probably be true for the foreseeable future, as CommonJS will be here to stay for at least five years.

It’s pretty difficult to tease out how to use this package with CommonJS, there are no docs or mention of it anywhere.

The best I can come up with to make it work is something like:

const module = await import('@keycloak/keycloak-admin-client');
const KcAdminClient = module.default.default;

I was only able to figure this out after putting breakpoints on and inspecting the module - it’s strange to have to do .default.default

Is there a way to improve the developer experience with CommonJS?

Version

18.0.0

Expected behavior

No response

Actual behavior

No response

How to Reproduce?

No response

Anything else?

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
jonkoopscommented, May 23, 2022

We are currently compiling this package in the CommonJS format, the quirkiness we are seeing here is a side-effect of the TypeScript compiler. Now that Node.js 12 is no longer supported we intend to compile to ESM instead.

There are no intentions to have this package contain both ESM and CommonJS entry points and we’d rather opt for a pure ESM package instead. Node.js provides interoperability between these two so that it should ‘just work’ so I expect this will improve the developer experience.

0reactions
claytongulickcommented, Sep 8, 2022

No, it’s not.

Are you sure? https://antfu.me/posts/publish-esm-and-cjs

I’ve bundled for both without any major issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Disadvantage of ES6 and CommonJS export convention
Babel will use module.exports as default export if the imported module is CommonJS, so I don't see how module.exports.default = Struct helps. Do ......
Read more >
Annoyed by the schism between CommonJS and ESM - Reddit
The arguments there are all weak. "It is risky" is only true if you use typescript wrong or use weird libraries. Most important...
Read more >
Default Exports in JavaScript Modules Are Terrible
In this article, I want to explain why default exports are a poor practice and lead to a worse developer experience. Consider an...
Read more >
ES modules are terrible - Hacker News
As an exercise, try to write a static analyser that simply ignores dynamic imports and just outputs a dependency graph of your static...
Read more >
Simon Høiberg on Twitter: "JavaScript Tip Avoid default ...
JavaScript Tip Avoid default exports. You will get a better developer experience, especially if you're using TypeScript. If you're using CommonJS, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found