JedWatson / JedWatson/react-select

Latest release broke Next.js project with mixed CJS/ESM modules

Open
#5,668 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

issue/bug-unconfirmed
Dominant language
TypeScript
Stars
28k
Forks
4.1k
PR merge metrics
No merged PRs in 30d

Description

The issue is reproducible by creating a starter Next.js app (using either the pages or the app router), and creating the following files:

First, a regular ESM module that imports and uses react-select

/* file: components/Select.jsx */

import Select from "react-select";

const options = [
  { value: "chocolate", label: "Chocolate" },
  { value: "strawberry", label: "Strawberry" },
  { value: "vanilla", label: "Vanilla" },
];

export const MyComponent = () => <Select options={options} />;

Then, a CJS module requires it and re-exports it (in my real-world use case, this module actually does something with the component rather than just re-exporting the original one, but that's not relevant here)

/* file: components/Select2.js */
const { MyComponent } = require("./select");

module.exports = {
  MyComponent,
};

And finally, a page imports and renders it

/* file: pages/testingPage.jsx */

import { MyComponent } from "../comps/select2.js";

export default () => (
  <>
    <MyComponent />
  </>
);

This results in an Invalid Component error (expected ... but got undefined) error, but only during server-side rendering. If we tell Next to only render this on the client, it works fine.

This worked fine before 5.7.3.

Trying to dig in a bit more, I discovered that in the generated server bundle (inside the .next dir) imports to react-select were being compiled to dynamic import() calls (the ones that resolve to promises), whereas in versions before 5.7.3 it got compiled to normal require calls, as expected.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue in a starter Next.js app using components/Select.jsx, components/Select2.js, and pages/testingPage.jsx. Compare the generated server bundle in .next between 5.7.3 and an earlier version, focusing on react-select imports becoming dynamic import() calls. Done means the mixed CJS/ESM example renders successfully during server-side rendering.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, next.js
Domain
build-system, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.