Jest cannot find 'react-dom/client' or '@testing-library/dom' for Testing Library
Cannot 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.
Fix it fast
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/dom2Match what you see, then jump to its fix
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-dom2Fix
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@123Verify
Run the tests again.
npx jestConfirmed by github.com
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/dom2Fix
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-dom3Verify
npm ls shows a single @testing-library/dom, and the tests run.
npm ls @testing-library/dom
npx jestConfirmed by github.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
npm cannot resolve @testing-library packages and their peer dependencies
when using create-react-app dependencies @testing-library could not resolve
@testing-library/react needs @testing-library/dom installed next to it as a peer dependency.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fix