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.

Unable to use package with Next.js: `ERR_REQUIRE_ESM`

See original GitHub issue

Describe the bug When using this package with next.js (tested 10-12), it fails to build, producing the error ERR_REQUIRE_ESM. I have not found a configuration where this issue does not persist.

I have also tried adding a custom .babelrc with the stock configuration to disable compilation with SWC, but the issue persists.

Further Logs
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/user/repos/projects/ndocs/node_modules/@primer/behaviors/dist/utils/index.js from /home/user/repos/projects/ndocs/node_modules/@primer/react/lib/hooks/useOpenAndCloseFocus.js not supported.
Instead change the require of index.js in /home/user/repos/projects/ndocs/node_modules/@primer/react/lib/hooks/useOpenAndCloseFocus.js to a dynamic import() which is available in all CommonJS modules.

To Reproduce Steps to reproduce the behavior:

  1. Create a new next.js app using the cli a. npx create-next-app@latest --typescript

  2. Cd into the project folder and add this package to the project: a. yarn add @primer/react react react-dom styled-components

  3. Replace the contents of src/_app.tsx with the following:

    Snippet
      import { ThemeProvider } from "@primer/react";
      import type { AppProps } from "next/app";
      import "../styles/globals.css";
      
      function MyApp({ Component, pageProps }: AppProps) {
          return (
              <ThemeProvider>
                  <Component {...pageProps} />
              </ThemeProvider>
          );
      }
      
      export default MyApp;
    
  4. Build the project with yarn build, observe console as it fails.

Expected behavior Project builds successfully without errors.

Screenshots N/A, refer to logs.

Desktop (please complete the following information):

  • OS: Ubuntu 20.04.3 LTS
  • Browser: N/A
  • Version: 34.1.0

Additional context

package.json
{
  "name": "ndocs",
  "private": true,
  "scripts": {
      "dev": "next dev",
      "build": "next build",
      "start": "next start",
      "lint": "next lint"
  },
  "dependencies": {
      "@primer/react": "^34.1.0",
      "next": "^12.0.7",
      "react": "17.0.2",
      "react-dom": "17.0.2",
      "styled-components": "^5.3.3"
  },
  "devDependencies": {
      "@types/node": "17.0.4",
      "@types/react": "17.0.38",
      "eslint": "8.5.0",
      "eslint-config-next": "12.0.7",
      "typescript": "4.5.4"
  }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
pksjcecommented, Jan 17, 2022

Took a stab at this since it was relevant for https://github.com/primer/react/pull/1771

The core of the problem is that nextjs does not compile anything in node_modules. @primer/behaviors has only an esm bundle available.

Technically https://github.com/martpie/next-transpile-modules#readme should solve the problem but it still threw the same error.

So I built up @primer/behaviors in a similar manner to @primer/react in the linked PR. It seems to have solved the issue! 🍏 Next steps are to polish the PR and test it thoroughly to prevent any edge cases.

1reaction
siddharthkpcommented, Jan 5, 2022

Can confirm this is related to https://github.com/primer/react/pull/1700

Read more comments on GitHub >

github_iconTop Results From Across the Web

ERR_REQUIRE_ESM and Package.json error on Next.js. ...
This error is fixed when removing "type": "module" from package.json . Support for using ESM-only node_modules is still fine. The reason "type": ...
Read more >
Error: Must use import to load ES Module · Issue #25454
FWIW, the latest NextJS@11 produces a similar webpack error when importing of an ESM-only npm package. 12
Read more >
How to Bypass ES Modules Errors in Next.js with Dynamic ...
js. The error in the image below shows that we can't use the CommonJS approach to import a module. serialize-1.
Read more >
[ERR_REQUIRE_ESM] What is this? Package.json error ...
I had taken a LONG break from Node and Next.js to the point that I don't ... Error [ERR_REQUIRE_ESM]: Must use import to...
Read more >
arcgis/core with Next JS Error [ERR_REQUIRE_ESM]
Solved: My intention is to use ArcGis API in a Next JS application. ... which defines all .js files in that package scope...
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