Maaz Zindani

Loading the index…

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

npm cannot resolve @testing-library packages and their peer dependencies

Errorwhen using create-react-app dependencies @testing-library could not resolve

Short 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.

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

Fix it fast

  1. 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-dom
  2. 2Match what you see, then jump to its fix

    1. 1npm names @testing-library/dom as a peer that it could not findThe @testing-library/dom peer dependency is not installedFix
    2. 2The same install works on another machine or in another projectnpm is set to fail on any peer conflictFix
    3. 3The npm output lists two different required versions of the same packageTwo packages ask for peer ranges that cannot both be metFix

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/dom

2Fix

Install both together, as the docs show.

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

3Verify

The install finishes and the list shows both packages.

npm ls @testing-library/react @testing-library/dom

Confirmed by testing-library.com

Did this fix it?

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-deps

2Fix

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 false

3Verify

Run the install again.

npm install

Confirmed by docs.npmjs.com

Did this fix it?

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-dom

2Fix

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-deps

3Verify

The install completes. Run the tests to confirm that the mismatch does not break them.

npm test

Confirmed by docs.npmjs.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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