Cannot find module 'react'
FrancisVila opened this issue · comments
I get an error
Cannot find module 'react' from '../node_modules/@wojtekmaj/enzyme-adapter-react-17/build/ReactSeventeenAdapter.js'
I installed react both in dependencies and devdependencies
Here's my test file:
import React, { useEffect, useState } from "react";
import Enzyme from 'enzyme';
import EnzymeAdapter from '@wojtekmaj/enzyme-adapter-react-17';
import App from './App'
Enzyme.configure({ adapter: new EnzymeAdapter() });
test ('renders learnt react link', ()=> {
const {getByText} = render(<App />);
const title = getByText("Search and Select Snippets");
expect (title).toBeInTheDocument();
})
Here's my package.json:
{
"name": "axwayprerequisites",
"homepage": ".",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"axios": "^0.21.0",
"bootstrap": "^4.5.3",
"dompurify": "^2.2.0",
"env-cmd": "^10.1.0",
"jquery": "^3.5.1",
"logrocket": "^1.0.14",
"react-bootstrap": "^1.4.0",
"react-dom": "^17.0.1",
"react-hook-form": "^6.9.6",
"react-scripts": "4.0.0",
"react-select": "^3.1.0",
"react-tabs": "^3.1.1",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"start:local": "env-cmd -f ./.env.local react-scripts start",
"start:testlocal": "env-cmd -f ./.env.testlocal react-scripts start",
"start:test_remoteserver_localclient": "env-cmd -f ./.env.test_remoteserver_localclient react-scripts start",
"test": "react-scripts test",
"eject": "react-scripts eject",
"build": "env-cmd -f ./.env.local react-scripts build",
"build:local": "env-cmd -f ./.env.local react-scripts build",
"build:prod": "env-cmd -f ./.env.prod react-scripts build",
"build:remoteTestServer": "env-cmd -f ./.env.remoteTestServer react-scripts build",
"toto": "env-cmd -f ./.env.toto node toto.js"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"enzyme": "^3.11.0",
"react": "^17.0.1"
}
}
"react" probably shouldn't be in devDependencies, but rather in dependencies.
Hi wojtekmaj , thanks for answering
react was initially in dependencies. I added it to devdependencies because I read somewhere that that could solve this kind of problem.
Anyway I tried with a) in dependencies, b) in devdependencies, c) in both, and the error persists.
I'm sure that react should be in dependencies.
Consider removing your package-lock.json
or yarn.lock
(depending on the package manager used, but given the weirdness of this issue I suppose it's NPM ;) ), remove node_modules
and try installing your app again.
Check npm list react
if you didn't get multiple copies of React in your repository.
Can it reside in the peerDependencies
instead? I have a library which gets included in other local React project.
In adapter they are in peerDependencies
. We were discussing OP's own package.json.
I believe we are talking about the same thing.
Can the React dependency reside in the peerDependencies
instead of the dependencies
, as discussed above?
I no longer have this problem, not sure how it occurred or how it disappeared. I'm closing it. My thanks to @nemanjamilosavljevic-newtron and @wojtekmaj
I no longer have this problem, not sure how it occurred or how it disappeared. I'm closing it. My thanks to @nemanjamilosavljevic-newtron and @wojtekmaj