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.

[CRA v5] Jest + React-markdown = Must use import to load ES Module

See original GitHub issue

Describe the bug

Running a CRA test rendering react-markdown leads to an error message “Must use import to load ES Module:”

A (somewhat) related issue in https://github.com/remarkjs/react-markdown/issues/635 seems to redirect to https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c, which in turn states “Create React App doesn’t yet fully support ESM. I would recommend opening an issue on their repo with the problem you have encountered. One known issue is #10933.”

/cc @ChristianMurphy

Did you try recovering your dependencies?

Yes

Which terms did you search for in User Guide?

esm

Environment

Windows

Steps to reproduce

Full repro case is available at

  1. Clone https://github.com/nulltoken/repro-react-markdown-cra-esm
  2. run yarn test

Output of yarn test can be reviewed at https://github.com/nulltoken/repro-react-markdown-cra-esm/runs/4841320780?check_suite_focus=true

Expected behavior

I’d very much like the test not to fail because of an import issue 😕

Actual behavior

image

Reproducible demo

(Paste the link to an example project and exact instructions to reproduce the issue.)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:11

github_iconTop GitHub Comments

6reactions
KieranDotCocommented, Jan 17, 2022

Not sure if this helps but I managed to get my react-markdown tests to run with CRA by modifying the test command

react-scripts test --maxWorkers=2 --transformIgnorePatterns \"node_modules/(?!react-markdown)/\" --env=jsdom
5reactions
martenmatrixcommented, Jun 4, 2022

@jcgentr and @esetnik answers didn’t work for me. However, they pointed me in the correct direction. Especially because those solutions work for projects created with create-react-app. I needed to edit the regex pattern to the following.

"jest": {
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\](?!(react-markdown|vfile|vfile-message|markdown-table|unist-.*|unified|bail|is-plain-obj|trough|remark-.*|rehype-.*|html-void-elements|hast-util-.*|zwitch|hast-to-hyperscript|hastscript|web-namespaces|mdast-util-.*|escape-string-regexp|micromark.*|decode-named-character-reference|character-entities|property-information|hast-util-whitespace|space-separated-tokens|comma-separated-tokens|pretty-bytes|ccount|mdast-util-gfm|gemoji)).+\\.(js|jsx|mjs|cjs|ts|tsx)$"
    ]
  }

Basically, I just added these patterns:

  • rehype-*
  • hast-util-.*
  • mdast-util-gfm
  • web-namespaces
  • zwitch
  • hast-to-hyperscript
  • html-void-elements
  • gemoji

If running the tests still fails for you because Jest encountered an unexpected token, just try to add the folder, where the error occurred, from the node_module folder. Unfortunately, sometimes the error message does not directly point to the correct package, which should be transformed. For example, I always got an error message which pointed to the mdast-util-gfm-autolink-literal package, however, the error actually happened because mdast-util-gfm was not transformed. Just try to google the error. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Must use import to load ES Module: D:\node_modules\react ...
I solved this by just pinning react-markdown to version 6. With version 7 of react-markdown they moved to ESM only. There's a explanation...
Read more >
“syntaxerror cannot use import statement outside a module ...
Answers related to “syntaxerror cannot use import statement outside a module react jest”. cannot use import statement outside a module · SyntaxError: Cannot ......
Read more >
[Solved]-Error: Must use import to load ES Module
Coding example for the question Error: Must use import to load ES Module: D:\node_modules\react-markdown\index.js require() of ES modules is not ...
Read more >
Getting started - MDX
Our packages are also ESM only, so they have to be import ed instead of ... If you're using webpack (or Create React...
Read more >
the `@emotion/core` package has been renamed to ... - You.com
docz: 2.4, react, and react-dom 17 in my project, when i run docz dev have the problem. so i yarn add @emotion/core, but...
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