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.

CJS + ESM Question/discussion

See original GitHub issue

React Popper pulls in via import isEqual from 'react-fast-compare'; and this is failing in Rollupjs which states default is not exported from [module].

Would it be possible for the package that is released to npm contain a cjs and an esm file? This would allow for require() statements as well as import.

I’ll happily put a PR together if this is desired, but wanted to know where the thoughts were in the project before putting in the effort.

I have this working now using patch-package with the following patch:

diff --git a/node_modules/react-fast-compare/index.js b/node_modules/react-fast-compare/index.js
index 1301ff0..3fa4874 100644
--- a/node_modules/react-fast-compare/index.js
+++ b/node_modules/react-fast-compare/index.js
@@ -115,7 +115,7 @@ function equal(a, b) {
 }
 // end fast-deep-equal
 
-module.exports = function isEqual(a, b) {
+function isEqual(a, b) {
   try {
     return equal(a, b);
   } catch (error) {
@@ -132,3 +132,5 @@ module.exports = function isEqual(a, b) {
     throw error;
   }
 };
+
+export default isEqual;

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
apipkincommented, Jun 3, 2020

I can confirm, putting the commonjs plugin before the babel plugin solved this. Not certain why that didn’t work yesterday during my testing, but glad it’s working today… ship it, amiright?

2reactions
kittencommented, Jun 3, 2020

Usually the commonjs module in Rollup should be placed pretty early in the pipeline, like after resolve, so it may be as simple as that in this case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What the heck are CJS, AMD, UMD, and ESM in Javascript?
ESM is the best module format thanks to its simple syntax, async nature, and tree-shakeability. UMD works everywhere and usually used as a ......
Read more >
CJS vs ESM - Andrea Giammarchi - Medium
The conclusion, here, is that ESM is a more secure module system than CJS and last, but not least, ESM is about syntax...
Read more >
Working with node and only esm packages #7872 - nrwl/nx
Workaround: npm install --save-dev webpack-node-externals; Create webpack.config.cjs in the application directory: // @filename ...
Read more >
Publish ESM and CJS in a single package - Anthony Fu
A short tutorial of shipping both ESM and CJS dual formats in a single NPM package.
Read more >
What are CJS, AMD, UMD, and ESM in Javascript? - Igor Irianto
What Are CJS, AMD, UMD, and ESM in Javascript? July 21, 2019. In the beginning, Javascript did not have a way to import/export...
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