kenpeter
kenpeter

Reputation: 8274

Cannot find module 'react' from 'pure.js'?

When running yarn test in package.json which is

CI=true react-app-rewired test --coverage

I got following error

Test suite failed to run

    Cannot find module 'react' from 'pure.js'

    However, Jest was able to find:
        './pure.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['web.js', 'js', 'web.ts', 'ts', 'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
      at Object.<anonymous> (../../node_modules/@testing-library/react/dist/pure.js:28:37)

Not sure why getting react from pure.js, any idea?

Upvotes: 12

Views: 9987

Answers (3)

Louay Al-osh
Louay Al-osh

Reputation: 3405

It's related to Version of @testing-library/react not being compatible with your react version

as mentioned here

I think it's because your using the newer version of @testing-library/react , just try installing version 12.1.2 (or 12.x)

[quote edited by me]

And as Kalm mentioned here too

if it's a mono repo project, you have to take care of every package.json file.

in my case(in a monorepo project), someone has installed a higher version of @testing-library/react which will not work with my current react version, so the solution was to trace every package.json and check what version of @testing-library/react I was using, then make sure it's using a lower version

Upvotes: 0

kalm42
kalm42

Reputation: 863

Jest is unable to find 'react' because it is either not installed or the closest package.json does not have react as a dependency (if it's a monorepo).

Upvotes: 1

Shubham Verma
Shubham Verma

Reputation: 9933

I was also facing the same issue, then I installed "react-test-renderer": "^16.13.1" and the issue is resolved. So you can try this also just installed this dependency by command npm install react-test-renderer --save.

As you can see in the below image:

enter image description here

Upvotes: 13

Related Questions