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.

Use ESM for build

See original GitHub issue

I have been successfully using react-extras v0.7.1 for the past year or so. Today, I tried to upgrade to a more recent version but the build failed. Rollup is having trouble importing components from react-extras v0.9.0 and later due to the convoluted way that Babel is defining the CommonJS exports.

Click here to see the strack trace from Rollup
[!] Error: 'Choose' is not exported by node_modules/react-extras/dist/index.js
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
lib/components/payment/payment-preview.jsx (3:9)
1: import React, { useEffect, useState } from 'react';
2: import PropTypes from 'prop-types';
3: import { Choose } from 'react-extras';
            ^
4: import { Dimmer, Loader, Message } from 'semantic-ui-react';
5: import { load } from '../../constants';
Error: 'Choose' is not exported by node_modules/react-extras/dist/index.js
    at error (/Users/sholladay/Code/<redacted>/client/node_modules/rollup/dist/rollup.js:5363:30)
    at Module.error (/Users/sholladay/Code/<redacted>/client/node_modules/rollup/dist/rollup.js:9701:9)
    at handleMissingExport (/Users/sholladay/Code/<redacted>/client/node_modules/rollup/dist/rollup.js:9618:21)
    at Module.traceVariable (/Users/sholladay/Code/<redacted>/client/node_modules/rollup/dist/rollup.js:10011:17)
    at ModuleScope.findVariable (/Users/sholladay/Code/<redacted>/client/node_modules/rollup/dist/rollup.js:8678:39)
    at ReturnValueScope.findVariable (/Users/sholladay/Code/<redacted>/client/node_modules/rollup/dist/rollup.js:3054:38)
    at ChildScope.findVariable (/Users/sholladay/Code/<redacted>/client/node_modules/rollup/dist/rollup.js:3054:38)
    at Identifier$1.bind (/Users/sholladay/Code/<redacted>/client/node_modules/rollup/dist/rollup.js:4392:40)
    at CallExpression$1.bind (/Users/sholladay/Code/<redacted>/client/node_modules/rollup/dist/rollup.js:3137:31)
    at CallExpression$1.bind (/Users/sholladay/Code/<redacted>/client/node_modules/rollup/dist/rollup.js:6643:15)

Notably, with v0.9.0, node_modules/react-extras/dist/index.js contains:

Object.defineProperty(exports, "Choose", {
  enumerable: true,
  get: function get() {
    return _choose.default;
  }
});

Whereas, with previous versions, it was simply:

exports.Choose = Choose;

This change was introduced in https://github.com/sindresorhus/react-extras/commit/48b3170d8581dae942841f9c1c7dc9a959c15d8f.

Needless to say, the former is much harder to statically analyze than the latter. We could try to blame Rollup for not being sophisticated enough, but it doesn’t surprise me that it is having trouble with this.

I think the best solution would be for react-extras to publish a build in ES modules format. That way it’s easier for modern bundlers to import.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sindresorhuscommented, Feb 6, 2020
0reactions
sholladaycommented, Feb 6, 2020

I see. I assumed that we would still want to transpile the JSX syntax. If we remove Babel, we lose that, and won’t that make it hard for people to use since running Babel on node_modules is uncommon?

I suppose we could just not use JSX. I only see a little bit of it in this package anyway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using ECMAScript modules (ESM) with Node.js
Learn about using ES modules in Node.js today and get a closer look at how you can migrate your codebase to make use...
Read more >
build-esm - npm
Start using build-esm in your project by running `npm i build-esm`. There are no other projects in the npm registry using build-esm.
Read more >
How to Create a Hybrid NPM Module for ESM and CommonJS.
Build the source twice, once for ESM and once for CommonJS. We use Typescript as our transpiler, and author in ES6/ES-Next or Typescript....
Read more >
How to use ESM on the web and in Node.js - ByteofDev
ESM (ECMAScript Modules) is a modern module format with many ... is very helpful as it allows you to use NPM packages built...
Read more >
Getting Started with (and Surviving) Node.js ESM
The Node.js ecosystem is shifting towards ESM. We examine what lies in store for application and library authors, learn some of challenges ...
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