Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Jest cannot find 'react-dom/client' or '@testing-library/dom' for Testing Library

ErrorCannot find module 'react-dom/client' from 'node_modules/@testing-library/react/dist/pure.js'

Short answer

Two causes cover almost every case. React Testing Library 13 and newer needs React 18, because it imports react-dom/client; on React 17 use version 12 or upgrade React. Since version 16 the package @testing-library/dom is a peer you must install yourself.

  • Applies to Node.js 22, Jest 29, @testing-library/react 16
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the installed versions that matter. The next steps depend on react, react-dom and the Testing Library packages.

    npm ls react react-dom @testing-library/react @testing-library/dom
  2. 2Match what you see, then jump to its fix

    1. 1The missing module is react-dom/clientReact Testing Library 13 or newer on React 17 or olderFix
    2. 2The missing module is @testing-library/dom, from react or user-event@testing-library/dom is not installed as a peerFix

What does the error look like?

  • Cannot find module 'react-dom/client' from 'node_modules/@testing-library/react/dist/pure.js'
  • Cannot find module '@testing-library/dom' from 'node_modules/@testing-library/react/dist/pure.js'
  • Cannot find module '@testing-library/dom' from 'node_modules/@testing-library/user-event/dist/click.js'

Cause 1 of 2

React Testing Library 13 or newer on React 17 or older

1Check

The README says React Testing Library 13 and newer require React 18. react-dom/client does not exist before that.

npm ls react react-dom

2Fix

Upgrade react and react-dom to 18 or newer, or stay on React 17 and install version 12 of the library.

npm install react@latest react-dom@latest
# or, staying on an older React
npm install --save-dev @testing-library/react@12

3Verify

Run the tests again.

npx jest

Confirmed by github.com

Did this fix it?

Cause 2 of 2

@testing-library/dom is not installed as a peer

1Check

From version 16 the library needs @testing-library/dom installed explicitly. It was moved to a peer dependency to avoid two conflicting copies, which had caused bugs with user-event.

npm ls @testing-library/dom

2Fix

Install it next to the library. Add @types/react-dom too if you type-check the test files.

npm install --save-dev @testing-library/dom
npm install --save-dev @types/react-dom

3Verify

npm ls shows a single @testing-library/dom, and the tests run.

npm ls @testing-library/dom
npx jest

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Match the library major version to your React version before upgrading either.
  • Install @testing-library/react and @testing-library/dom in one command.
  • Run npm ls after dependency changes to spot duplicate or missing copies.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.