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.

Can't use react-dnd ESM library: ERR_REQUIRE_ESM

See original GitHub issue

What version of Remix are you using?

1.4.1 – Blues stack

Steps to Reproduce

import { DndProvider, useDrag, useDrop } from "react-dnd";
import { HTML5Backend } from "react-dnd-html5-backend";
import { TouchBackend } from "react-dnd-touch-backend";

Expected Behavior

No errors

Actual Behavior

I recently migrated my remix project to the blues stack (1.4.1). I’m using react-dnd in my project. When I executed npm run dev, I got the following warnings + error:

react-dnd is possibly an ESM only package and should be bundled with "serverDependenciesToBundle in remix.config.js.
react-dnd-html5-backend is possibly an ESM only package and should be bundled with "serverDependenciesToBundle in remix.config.js.
react-dnd-touch-backend is possibly an ESM only package and should be bundled with "serverDependenciesToBundle in remix.config.js.
/Users/benjamin/Projects/horeka/horeka/build/index.js:4591
var import_react_dnd = require("react-dnd");
                       ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/benjamin/Projects/horeka/horeka/node_modules/react-dnd/dist/index.js from /Users/benjamin/Projects/horeka/horeka/build/index.js not supported.
Instead change the require of /Users/benjamin/Projects/horeka/horeka/node_modules/react-dnd/dist/index.js in /Users/benjamin/Projects/horeka/horeka/build/index.js to a dynamic import() which is available 

I followed the warnings advice, adding stuff to serverDependenciesToBundle. Several iterations led me to end up with this remix.conf.js file:

/**
 * @type {import('@remix-run/dev').AppConfig}
 */
module.exports = {
  cacheDirectory: "./node_modules/.cache/remix",
  ignoredRouteFiles: [".*", "**/*.css", "**/*.test.{js,jsx,ts,tsx}"],
  serverDependenciesToBundle: [
    /^react-dnd.*/,
    /^@react-dnd.*/,
    /^dnd-core/,
  ],
};

But I still get an error:

/Users/benjamin/Projects/horeka/horeka/build/index.js:5173
var scope = typeof global !== "undefined" ? global : self;
                  ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/benjamin/Projects/horeka/horeka/node_modules/@react-dnd/asap/dist/index.js from /Users/benjamin/Projects/horeka/horeka/build/index.js not supported.
Instead change the require of /Users/benjamin/Projects/horeka/horeka/node_modules/@react-dnd/asap/dist/index.js in /Users/benjamin/Projects/horeka/horeka/build/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/benjamin/Projects/horeka/horeka/build/index.js:5173:19)
    at Server.<anonymous> (/Users/benjamin/Projects/horeka/horeka/build/server.js:69:3)
    at Object.onceWrapper (node:events:639:28)
    at Server.emit (node:events:520:28)
    at Server.emit (node:domain:475:12)
    at emitListeningNT (node:net:1364:10) {
  code: 'ERR_REQUIRE_ESM'
}
ERROR: "dev:server" exited with 1.

In my old project setup ("remix": "^1.2.3"), it was working flawlessly.

Existing issues has been closed an redirected to #109 but has been closed too: https://github.com/remix-run/remix/issues/109#issuecomment-1104059860

But in this case, serverDependenciesToBundle doesn’t seem to help.

Any help would be greatly appreciated 🙏

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
kilimancommented, May 2, 2022

I added a command to my rmx-cli project to get-esm-packages. It scans the packages you provide and looks at the package.json files. If it’s type:module, it’ll add it to bundle list. It recursively scans all dependencies.

npx rmx get-esm-packages react-dnd react-dnd-html5-backend react-dnd-touch-backend
{
  'react-dnd',
  '@react-dnd/invariant',
  '@react-dnd/shallowequal',
  'dnd-core',
  '@react-dnd/asap',
  'react-dnd-html5-backend',
  'react-dnd-touch-backend'
}
0reactions
kilimancommented, May 2, 2022

You can find the latest version here https://github.com/kiliman/rmx-cli

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error [ERR_REQUIRE_ESM]: require() of ES Module not ...
You can solve the "[ERR_REQUIRE_ESM]: require() of ES Module not supported" by doing one of two things: Use ESM - use import foo...
Read more >
Import ES module in Next.js ERR_REQUIRE_ESM
Using next-transpile-modules to transpile ESM libraries is no longer required. Before Next.js 12. Since ky is exported as ESM you can transpile ...
Read more >
react-dnd - npm
Start using react-dnd in your project by running `npm i react-dnd`. There are 2581 other projects in the npm registry using react-dnd.
Read more >
can't resolve 'react/jsx-runtime' - You.com | The AI Search ...
When compiling a library to esm, generated imports for react/jsx-runtime are incorrect and don't resolve. This causes CRA to throw an error. Configuration...
Read more >
Invalid Hook Call Warning - React
You might be using a version of react-dom (< 16.8.0) or react-native (< 0.59) that doesn't yet support Hooks. You can run npm...
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