npm cannot resolve @testing-library packages and their peer dependencies
when using create-react-app dependencies @testing-library could not resolveShort answer
@testing-library/react needs @testing-library/dom installed next to it as a peer dependency. If it is missing, or if two packages ask for peers that cannot both be met and npm runs in strict mode, the install stops. Install both packages together, check the strict-peer-deps setting, and use legacy-peer-deps only as a last resort.
Fix it fast
1Find out which cause you have
Read the full npm error. It names the package that asks for the peer and the version it found. Then list what is installed.
npm ls @testing-library/react @testing-library/dom react react-dom2Match what you see, then jump to its fix
- 1
npm names @testing-library/dom as a peer that it could not findThe @testing-library/dom peer dependency is not installedFix - 2
The same install works on another machine or in another projectnpm is set to fail on any peer conflictFix - 3
The npm output lists two different required versions of the same packageTwo packages ask for peer ranges that cannot both be metFix
- 1
What does the error look like?
when using create-react-app dependencies @testing-library could not resolve
Cause 1 of 3
The @testing-library/dom peer dependency is not installed
1Check
The React Testing Library install docs list @testing-library/dom as a peer dependency that must be installed with it.
npm ls @testing-library/dom2Fix
Install both together, as the docs show.
npm install --save-dev @testing-library/react @testing-library/dom3Verify
The install finishes and the list shows both packages.
npm ls @testing-library/react @testing-library/domConfirmed by testing-library.com
Cause 2 of 3
npm is set to fail on any peer conflict
1Check
By default npm resolves a peer conflict with the nearest non-peer spec and prints a warning. With strict-peer-deps set to true, that warning becomes a failure.
npm config get strict-peer-deps2Fix
Turn the setting off for this project, or fix the version range that conflicts, which is the better long-term fix.
npm config set strict-peer-deps false3Verify
Run the install again.
npm installConfirmed by docs.npmjs.com
Cause 3 of 3
Two packages ask for peer ranges that cannot both be met
1Check
Find which dependency pins the older range.
npm ls react react-dom2Fix
Upgrade the package with the outdated range. As a last resort, the legacy-peer-deps option makes npm ignore peerDependencies entirely; the docs warn that it does not enforce the peerDependencies contract.
npm install --legacy-peer-deps3Verify
The install completes. Run the tests to confirm that the mismatch does not break them.
npm testConfirmed by docs.npmjs.com
How do I stop it coming back?
- Install @testing-library/react and @testing-library/dom in one command.
- Treat legacy-peer-deps as a temporary workaround, and record why it is on.
- Replace packages whose peer ranges no longer allow your React version.
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
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 fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix