Next.js 12 breaks when used with theme-ui giving "code: 'ERR_REQUIRE_ESM'"

60.7k Views Asked by At

I'm using Next.js 12 and this error pops up after I created a theme using theme-ui

enter image description here

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: E:\fm-nextjs\node_modules\@mdx-js\react\index.js
require() of ES modules is not supported.
require() of E:\fm-nextjs\node_modules\@mdx-js\react\index.js from E:\fm-nextjs\node_modules\@theme-ui\mdx\dist\theme-ui-mdx.cjs.dev.js 
is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from E:\fm-nextjs\node_modules\@mdx-js\react\package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)    at Module.require (internal/modules/cjs/loader.js:961:19)       
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (E:\fm-nextjs\node_modules\@theme-ui\mdx\dist\theme-ui-mdx.cjs.dev.js:9:15)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)     
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12) {
  code: 'ERR_REQUIRE_ESM'
}

my package.json has following dependencies

  "dependencies": {
    "@emotion/react": "^11.7.1",
    "@emotion/styled": "^11.6.0",
    "@mdx-js/react": "^2.0.0",
    "@theme-ui/presets": "^0.13.1",
    "next": "^12.0.10",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "theme-ui": "^0.13.1"
  }

I tried removing "type": "module" from E:\fm-nextjs\node_modules\@mdx-js\react\package.json but it still gave errors.
How do I actually fix this. Seems like I have to manually change a bunch of files. Would be great if there is a workaround.

1

There are 1 best solutions below

2
On

Downgrade @mdx-js/react to 1.6.22

Usually in these kind of scenarios, it's good to start downgrading the latest installed packages (especially since @mdx-js seems to have had a major version increase)