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.

ERR_REQUIRE_ESM error in gatsby-recipes

See original GitHub issue

Preliminary Checks

Description

/Users/dangreen/github/react-chartjs-2/node_modules/gatsby-recipes/dist/graphql-server/server.js:52
var remarkMdx = require('remark-mdx');
                ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/dangreen/github/react-chartjs-2/node_modules/gatsby-recipes/node_modules/remark-mdx/index.js from /Users/dangreen/github/react-chartjs-2/node_modules/gatsby-recipes/dist/graphql-server/server.js not supported.
Instead change the require of index.js in /Users/dangreen/github/react-chartjs-2/node_modules/gatsby-recipes/dist/graphql-server/server.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/dangreen/github/react-chartjs-2/node_modules/gatsby-recipes/dist/graphql-server/server.js:52:17) {
  code: 'ERR_REQUIRE_ESM'
}

https://github.com/gatsbyjs/gatsby/blob/d04f563342f7679728738e623578dd712a34caca/packages/gatsby-recipes/package.json#L53

remark-mdx@^2.0.0-next.4 resolves to remark-mdx@2.0.0-rc.1 which is ESM only package

Reproduction Link

https://codesandbox.io/s/cocky-sound-og3zh?file=/package.json

Steps to Reproduce

  1. yarn add -D docz react react-dom
  2. yarn docz dev

Expected Result

All works without any errors.

Actual Result

ERR_REQUIRE_ESM error

Environment

System:
    OS: macOS 11.6
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.10.0 - /var/folders/h2/gn99zpsn25j5w47332yy8jfw0000gn/T/yarn--1635416469908-0.7042407843892959/node
    Yarn: 1.22.13 - /var/folders/h2/gn99zpsn25j5w47332yy8jfw0000gn/T/yarn--1635416469908-0.7042407843892959/yarn
    npm: 7.24.0 - ~/.nvm/versions/node/v16.10.0/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  Browsers:
    Chrome: 95.0.4638.54
    Firefox: 91.0.2
    Safari: 15.0

Config Flags

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:11
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

22reactions
Hannah-goodridgecommented, Nov 12, 2021

Any update on this? Its affecting me too.

7reactions
iChenLeicommented, Nov 23, 2021

Gatsby use rollup to bundle gatsby-recipes package,

here is rollup config -> packages/gatsby-recipes/rollup.config.js https://github.com/gatsbyjs/gatsby/blob/d04f563342f7679728738e623578dd712a34caca/packages/gatsby-recipes/rollup.config.js#L33-L38

All import Declaration will be transform to require Declaration

https://github.com/gatsbyjs/gatsby/blob/dcc32a7db3413c79c5d62f9f72d8aaafb39b58df/packages/gatsby-recipes/src/parser/index.js#L2

to

https://cdn.jsdelivr.net/npm/gatsby-recipes@1.2.0/dist/graphql-server/server.js

var remarkMdx = require('remark-mdx');

https://github.com/mdx-js/mdx/blob/main/packages/remark-mdx/package.json#L34

remark-mdx breaking change -> pr https://github.com/mdx-js/mdx/pull/1661

2021-11-23 22 55 34

The CommonJS module require always treats the files it references as CommonJS. Using require to load an ES module is not supported because ES modules have asynchronous execution. Instead, use import() to load an ES module from a CommonJS module.

https://nodejs.org/api/esm.html#require

sindresorhus’s The ESM move

https://github.com/sindresorhus/meta/discussions/15

I think more and more npm package will be esm-only in the future (e.g. https://github.com/sindresorhus/meta/discussions/15#discussioncomment-1263848), so gatsby need care this trends.

Temporary workaround

lock remark-mdx version in gatsby-recipes/package.json

- ^2.0.0-next.4
+ 2.0.0-next.9

But it’s not long-term solutions, esm is future. cc @wardpeet

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error [ERR_REQUIRE_ESM] Github deploy error
The issue was that I had 2 json files that it was trying to use as the node_modules installation script when deployed. Don't...
Read more >
Error [ERR_REQUIRE_ESM]: require() of ES Module not ...
The error message in the screenshot shows that the node-fetch package is causing the error. error err require esm require of es module....
Read more >
error [err_require_esm]: require() of es modul - You.com
I'm getting the error message: Error [ERRREQUIREESM]: require() of ES Module not supported Instead change the require to a dynamic import() which is...
Read more >
Using ES6 Module Syntax in Gatsby API Files on Gatsby Cloud
The purpose of this recipe is to demonstrate how to use ES6 module syntax in ... gatsby-node.js const requireEsm = require('esm')(module); ...
Read more >
Gatsby Changelog | 5.3.0
ES Modules (ESM) in Gatsby files; Improved error messages ... The new minimal required version is React 18. ... Deprecation of gatsby-recipes.
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