remix: [Bug]: importing ESM node_module causing ERR_REQUIRE_ESM

Which Remix packages are impacted?

  • remix (Remix core)
  • create-remix
  • @remix-run/architect
  • @remix-run/cloudflare-workers
  • @remix-run/dev
  • @remix-run/express
  • @remix-run/netlify
  • @remix-run/node
  • @remix-run/react
  • @remix-run/serve
  • @remix-run/server-runtime
  • @remix-run/vercel

What version of Remix are you using?

1.1.1

What version of Node are you using? Minimum supported version is 14.

v16.13.1

Steps to Reproduce

install @nodestrap/navbar using NPM: npm i @nodestrap/navbar

then import & render the <Navbar> import { Navbar } from ‘@nodestrap/navbar

… <Navbar></Navbar> …

Expected Behavior

it should run perfectly. Tested in nextjs / react app with no problem

Actual Behavior

error [ERR_REQUIRE_ESM]: require() of ES Module … from …index.js not supported. Instead change the require … to a dynamic import() which is available in all CommonJS modules.

I saw the problem is the generated build/index.js is a common js. It tries to import an ESM module using require('some-esm-module')

Is there any way to change the generated build/index.js as ESM module? CommonJS cannot import ESM but ESM can import CommonJS.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 23
  • Comments: 16 (1 by maintainers)

Most upvoted comments

+1 esm supprt is needed

serverModuleFormat: "esm" => still doesn’t work.

nowadays there are a lot of esm modules in npm, so all of them don’t work in remix. I’m going back to NextJS, not as futuristic as remix, but it works.

See documentation on gotchas for how to import ESM-modules: https://remix.run/docs/en/v1/pages/gotchas#importing-esm-packages

This might not work for everyone, but it worked for me.

module.exports = {
  ...
  serverModuleFormat: 'cjs', // or 'esm' both worked
  serverDependenciesToBundle: [
    /^@uppy.*/, // 👈🏼 notice the way its written
    /^nanoid.*/,
  ],
  ...
}

Seems you can use serverModuleFormat: "esm" in remix config to build the server in esm format, but ideally it should automatically output as esm if type: "module" is set in package.json (which is required to run esm module in Node.js unless you also output the server build as .mjs).

However currently Remix uses require to load remix.config.js and the server build, which won’t work with type: "module" 😅 IMO all the Remix packages and examples should be migrated to esm, and use import to load the server build and config file.

+1 esm support is needed

@yarapolana Thanks! You nailed it!! Your solution worked for me and I don’t get this error anymore.