Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERR_REQUIRE_ESM error in gatsby-recipes #33713

Closed
2 tasks done
dangreen opened this issue Oct 28, 2021 · 7 comments · Fixed by #34064
Closed
2 tasks done

ERR_REQUIRE_ESM error in gatsby-recipes #33713

dangreen opened this issue Oct 28, 2021 · 7 comments · Fixed by #34064
Labels
type: bug An issue or pull request relating to a bug in Gatsby

Comments

@dangreen
Copy link

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'
}

"remark-mdx": "^2.0.0-next.4",

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

@dangreen dangreen added the type: bug An issue or pull request relating to a bug in Gatsby label Oct 28, 2021
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Oct 28, 2021
@Hannah-goodridge
Copy link
Contributor

Any update on this? Its affecting me too.

@iChenLei
Copy link
Contributor

iChenLei commented Nov 23, 2021

Gatsby use rollup to bundle gatsby-recipes package,

here is rollup config -> packages/gatsby-recipes/rollup.config.js

output: {
dir: `dist`,
entryFileNames: `[name].js`,
format: "cjs",
sourcemap: true,
},

All import Declaration will be transform to require Declaration

import remarkMdx from "remark-mdx"

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 mdx-js/mdx#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

sindresorhus/meta#15

I think more and more npm package will be esm-only in the future (e.g. sindresorhus/meta#15 (comment)), 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

@LekoArts LekoArts removed the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Nov 23, 2021
@iChenLei
Copy link
Contributor

https://nodejs.org/api/esm.html#import-expressions

It’s worth noting that the only way to import ESM files from a CJS module is using dynamic import() calls. This can present challenges, but is the behavior in Node.js today.

@jonasdev
Copy link

Any updates?

@worldomkar
Copy link

yarn add @mdx-js/mdx @mdx-js/react

in newly created gatsby project moves @mdx-js/mdx versions from "v1" to ==> "^2.1.1".

Then this is seen:
Error: [ERR_REQUIRE_ESM]: require() of ES Module /Volumes/source/websites/new-gatsby-site/node_modules/@mdx-js/mdx/index.js from /Volumes/sourc e/websites/new-gatsby-site/node_modules/gatsby-plugin-mdx/utils/mdx.js not supported. Instead change the require of index.js in /Volumes/source/websites/new-gatsby-site/node_modules/gatsby-plugin-mdx/utils/mdx.js to a dynamic import() which is available in all CommonJS modules.

The expectation is newer versions of packages will be always compatible, updated and use the latest standards and best practices.
Can't wrap my head around why "v1" works but not "^2.1.1". Doesn't sound right.

If I manually edit package.json and revert @mdx-jx/mdx and @mdx-js/react versions to "v1", and run yarn install, it starts to work again.

I may be missing something very silly basic, but what??

@cgarrovillo
Copy link

Think this needs to be reopened

@TomaszWaszczyk
Copy link

I have probably the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants