Cannot find module 'babel-preset-react' - reactjs

This is my first React Native project. The repo is here.
When I start up expo, I get the error:
Cannot find module 'babel-preset-react'
Among others, it has these dependencies:
"expo": "^32.0.0",
"react": "16.8.4",
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.1.tar.gz",
"#babel/core": "7.3.4",
"babel-core": "^7.0.0-bridge.0",
"#babel/preset-react": "^7.0.0",
I found these four libraries in my app's /node_modules that each contain a dev dependency on "babel-preset-react":
hoist-non-react-statics, prop-types, react-input-autosize and
react-proxy.
None of these have newer versions that depend on "#babel/preset-react".

I had this a few, every time I install a new module/plugin.
My solution has been to delete node_modules folder and yarn install or npm install.
It stops the error. I'm not sure is the right solution (would be interested to know if it is), but works.

I had to change ['react'] to ['#babel/preset-react'] in .bablerc when upgrading from babel 6.x to 7.x:
{
"presets": [
"#babel/preset-env",
"#babel/preset-react",
]
}

This seems to be an issue to do with an expo project being in a bad state.
Expo is an express set of libs with the goal of simplifying development, but it is not compatible with most react native examples in the wild.
After further review, it looks like you may have followed a regular react native example in an Expo based project, without first ejecting.

I had a similar issue wherein I mistyped babel/preset-react as babel-preset-react in the babel config file.
Correcting the typo helped me resolve the issue.

I had this issue after an upgrade. I needed a restart and it was solved. Apparently some caching issue or running process which still uses an old dependency.

First of all make sure the babel config has '#babel/preset-react' in presets and not 'babel-preset-react'
Apart from that. this did it for me:
npm i #babel/preset-react
npm i #babel/core

Related

Component cannot be used as a JSX component in next.js [duplicate]

Ive been getting these strange type errors on my typescript project for certain packages.
Ex:
'TimeAgo' cannot be used as a JSX component.
Its instance type 'ReactTimeago<keyof IntrinsicElements | ComponentType<{}>>' is not a valid JSX element.
The types returned by 'render()' are incompatible between these types.
Type 'React.ReactNode' is not assignable to type 'import("/home/user/app/node_modules/#types/react-bootstrap-table-next/node_modules/#types/react/index").ReactNode'.
Type '{}' is not assignable to type 'ReactNode'.
I don't get these type errors on my local windows machine but they keep occurring in my linux virtual machine. I've deleted the project many times, cloned my repo and installed packages again in different versions of node and I still get the same type errors.
Checked node 12.18.3, 16.13.1
Here is some quick package json info:
"react-timeago": "^6.2.1",
"react-custom-scrollbars": "^4.2.1",
"react-custom-scrollbars-2": "^4.4.0",
"react": "^17.0.2",
"next": "^12.1.1",
"#types/react-custom-scrollbars": "^4.0.10",
"#types/react-timeago": "^4.1.3",
"#types/react": "^17.0.44",
"typescript": "^4.3.5"
"#types/node": "^14.18.12",
This happens on basic custom components:
MyTst.tsx
import TimeAgo from "react-timeago";
const Mytst = () => {
return (
<div>
<TimeAgo date={"02/02/2022"} />
</div>
);
};
export default Mytst;
I get this error for react-custom-scrollbars-2 as well. There seems to be an issue with matching the types correctly between the library which contains the component and the #types files associated with them. Anyone have any ideas on how to resolve these type errors?
Had the same issue.
Just add this
"resolutions": {
"#types/react": "17.0.2",
"#types/react-dom": "17.0.2"
},
to package.json file and run yarn command.
UPD: Just a bit detailed answer:
#types/react-dom has its own dependencies and one of them is #types/react with a version set to '*' - a major release, that now, probably, refers to 18.
Even if you specify some strict versions in your package.json (without ^) parent package might install its own duplicates of packages that you are already using for its own purposes.
By using resolutions we are specifying strict restrictions for dependencies of dependencies.
You will need to fix the version for #types/react package because many react libraries have dependency set as #types/react : "*", which will take the latest version of the package. (I suppose they just released version 18)
To do that you can add this in your package.json
"resolutions": {
"#types/react": "^17.0.38"
}
It will just work fine with yarn, but if you are using npm, then you will also need to add this in "scripts" section of your package.json
"preinstall": "npm install --package-lock-only --ignore-scripts && npx npm-force-resolutions"
This will simply use npm-force-resolutions package to fix versions from resolutions.
And then after doing npm install, your app should work just fine.
I known issued today
rm -rf node_modules
rm -rf yarn.lock
npm install
just used npm install sovled problem
but I don't know what happend
Had this with Styled Components. Resolutions didn't work for me so here's another
solution:
Brute force type casting:
const ThemeProviderFixed = ThemeProvider as unknown as React.FC<PropsWithChildren<{ theme: string }>>;
Sahil's answer is correct for npm and yarn, but if you are using pnpm, you need a slightly different setup in your package.json file:
"pnpm": {
"overrides": {
"#types/react": "^17.0.38"
}
}
If you are using a monorepo with several packages, this only works at the root package.json file of your repo. You can read more about it here in the pnpm docs.
I also had the same issue, so I updated npm version ^6.0.0 -> 8.0.0 and it was resolved.
Check npm version.
I came across this issue recently when upgrading to React 18 and forgetting to upgrade my corresponding types in devDependencies.
What worked for me was upgrading React types to match in the package.json file as shown
{
...
"dependencies": {
...
"react": "^18.1.0",
},
"devDependencies": {
...
"#types/react": "^18.0.8",
}
}
I recently ran into this with a Yarn monorepo where a Typescript create-react-app subproject had installConfig.hoistingLimits=workspace. This configuration prevents hoisting of dependencies to the top-level of the monorepo.
When running react-scripts start, the type checker was still looking at the top-level #types and not finding the matching #types/react for the version of React configured on the project. The fix that resolved it was to add the following to the tsconfig.json in the subproject:
"compilerOptions": {
...
"typeRoots": ["./node_modules/#types"]
},
...
This points Typescript at the type definitions that are installed for the specific sub-project.
Ok. I ended up fixing this problem but to warn you in advance, there wasn't a magical way to do this.
I basically uninstalled all the #types I think were the offenders. You can find this out by reading your error window. The key was this message in my original error above.
Type 'React.ReactNode' is not assignable to type 'import("/home/user/app/node_modules/#types/react-bootstrap-table-next/node_modules/#types/react/index").ReactNode'.
If you see where the node module types are pointing to and its not associated with your library, then remove it. In my case, my issue was the package TimeAgo and the type error was showing that types were assigned to a different package. So I uninstalled it and kept cycling through the errors until it went away.
Then I use npm run build to do type checks and instruct me which types I had to reinstall. (There is probably a better way to do this part but it worked for me even though it was tedious.)
This issue seems to happen when you have a ton of different libraries and their types that have similar dependencies and overtime if they aren't needed, don't do what I do and just keep them piled up in your package.json.
So if you think any type can have conflicts with your library, uninstall and see if the error goes away, and then reinstall if other type errors appear that say the dev type package is missing. I also had some #type packages as dependencies instead of devDependencies which I removed and moved back into dev. Don't know if that played a part.
When in doubt, remove all applicable types and see if the issue is resolved.
If you have older version of npm, just update npm to version > 8.0.0.
It worked for me.
I had npm version 6.x.x. I tried many solutions, but update npm to new version fix this problem easy.
for npm!
check which version of node and npm you have installed.
if you update to 8.x, npm provides you the same thing as resolution does for yarn but its"overrides".
update your package like this:
"overrides": {
"#types/react": "17.x.x",
"#types/react-dom": "17.x.x"
}
my npm and node versions were up to date on local instance, but not on git ci. After the update, it was working without to override the versions for react and react-dom.
Unfortunately in my case I can't use the most voted answer since I need #types18 since I need to use the latest hooks from react#18 like useId and I can't import them using #types/react#17 since they have no exported members for those hooks.
I was able to use latest types fixing the broken typed deps, thanks to #Chris Web' s answer. For example for the Redux Provider:
// temp fix due to #types/react#18
const Provider = _Provider as unknown as React.FC<{
children?: JSX.Element | string;
store: any;
}>;
The store: any is not ideal, but it's just a temp fix.
You can solve this issue by following the above solution for react
"resolutions": {
"#types/react": "17.0.2",
"#types/react-dom": "17.0.2"
},
and for react-native you don't need to add type for react-dom
"resolutions": {
"#types/react": "17.0.2",
},
After this if you still getting error for react types,
add the type package separetly for react
npm install -D #types/react#17.0.2
Note - don't add "^" in resolution as it will try to install the latest version of packages which may cause the same problem.
Problem
For those who have this type of error in the APP and are using yarn instead of npm.
Solution
Just add the to resolutions and preinstall script inside the package.json file and them execute yarn preinstall and yarn.
package.json
"resolutions": {
.....
"#types/react": "^17.0.38"
....
},
"scripts": {
......
"preinstall": "yarn --package-lock-only --ignore-scripts"
......
},
References
Source
Slightly different answer that worked for me (in case the above doesn't work for you). I had a node_modules folder in my user root. So my project folder looked like this:
~/checkouts/project/node_modules
but I also had a node_modules folder installed at the user root (probably an accident at some point):
~/node_modules
The way npm packages work is it crawls up the directory structure grabbing npm packages along the way. After removing this directory the problem went away.
Tested this on two windows machines one mac and one ubuntu. One win machines was fine (no error on build), the other wind machine gave this error on build. Mac was also fine but ubuntu was also giving this error on build. I was frustrated. Tested with different node versions but that did not help.
In the end had to update some types versions (not sure though if all four needed to be updated but after the update error disappeared):
"#types/react": "^16.14.3",
"#types/react-dom": "^16.9.10",
"#types/react-router": "^5.1.11",
"#types/react-router-dom": "^5.1.7",
to:
"#types/react": "^18.0.15",
"#types/react-dom": "^18.0.6",
"#types/react-router": "^5.1.18",
"#types/react-router-dom": "^5.3.3",
i tried it in two ways, with yarn's resolution it solved it, but then i deleted my yarn.lock and updated the react's type and it worked for me too without using yarn's resolution
yarn upgrade #types/react#^18.0.21
I posted a different answer but it was basically a duplicate answer so I'll provide another approach.
If you're using yarn, you can run yarn dedupe and it will make the necessary changes to your yarn.lock file. It will consolidate any references to the same package to resolve to the correct version. As you can see from here, the - lines are what were removed and the + line is modified and saved:
-"#types/react#npm:*, #types/react#npm:>=15, #types/react#npm:>=16.9.0":
- version: 17.0.43
- resolution: "#types/react#npm:17.0.43"
- dependencies:
- "#types/prop-types": "*"
- "#types/scheduler": "*"
- csstype: ^3.0.2
- checksum: 981b0993f5b3ea9d3488b8cc883201e8ae47ba7732929f788f450a79fd72829e658080d5084e67caa008e58d989b0abc1d5f36ff0a1cda09315ea3a3c0c9dc11
- languageName: node
- linkType: hard
-
-"#types/react#npm:^18.0.0":
+"#types/react#npm:*, #types/react#npm:>=15, #types/react#npm:>=16.9.0, #types/react#npm:^18.0.0":
"#types/react#npm:*, #types/react#npm:>=15, #types/react#npm:>=16.9.0"
was consolidated into
"#types/react#npm:*, #types/react#npm:>=15, #types/react#npm:>=16.9.0, #types/react#npm:^18.0.0"
In my case, I put the wrong type of value inside a div.
Error
Reason
Fix

Unable to run eslint in Next.js

I'm trying to set up eslint on my Next.js project. I followed the guide here https://nextjs.org/docs/basic-features/eslint
When I run yarn lint, I receive the error error - ESLint must be installed.
In my "devDependencies" in package.json, I have the following:
"eslint": "^8.0.1",
"eslint-config-next": "11.1.2"
I tried running yarn add --dev eslint, but I still receive the same error after running the lint command.
Edit:
This is an existing project. Was created with yarn create next-app.
Versions for next and react below
"next": "^11.1.2",
"react": "^17.0.2"
Node v14.18.1
ESLint v8 introduced breaking changes that impact compatibility with Next. Downgrading to 7.32.0 seems to work for most people.
The Next team is currently working on adding v8 support.

dependency tree error using create-react-app and storybook

TLDR:
how can I instruct storybook to use babel-loader v8.1.0 OR force react-scripts to use babel-loader v^8.2.2 ?
Details
I Develop a lib with ./example folder which is itself project created with create-react-app. I wanted to add storybook in addition to the normal example pages, so I installed storybook.
after installing storybook I can no longer start the example project with yarn start or the story book with yarn storybook.
There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.
The react-scripts package provided by Create React App requires a dependency:
"babel-loader": "8.1.0"
Don't try to install it manually: your package manager does it automatically.
However, a different version of babel-loader was detected higher up in the tree:
D:\Eliav\projects\git projects\react-xarrows\examples\node_modules\babel-loader (version: 8.2.2)
Manually installing incompatible versions is known to cause hard-to-debug issues.
If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.
To fix the dependency tree, try following the steps below in the exact order:
1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
2. Delete node_modules in your project folder.
3. Remove "babel-loader" from dependencies and/or devDependencies in the package.json file in your project folder.
4. Run npm install or yarn, depending on the package manager you use.
well I know what the issue but I don't know how to fix it:
I'm using react-scripts v4.0.3 which for unknown reason requiring exactly babel-loader v8.1.0. i can see this it in yarn.lock:
react-scripts#^4.0.1:
version "4.0.3"
...
dependencies:
...
babel-loader "8.1.0"
and storybook requiring babel-loader v8.2.2 or above:
"#storybook/builder-webpack4#6.2.9":
version "6.2.9"
...
dependencies:
...
babel-loader "^8.2.2"
already tried
what is written in the error above.
hoped that yarn upgrade would upgrade babel-loader from v8.1.0 to v8.2.2 but it does not work because react-scripts require exactly v8.1.0
a workaround that worked
Create a .env file in your project directory and include SKIP_PREFLIGHT_CHECK=true in the file.
but I want to avoid it. is it possible?
So for anyone to whom this is still unclear.
I used create-react-app to create a new app
I added storybook using npx sb init
then they clashed.
SOLUTION:
yarn add babel-loader#8.1.0
UPDATE:
The error you often see is that CRA (create-react-app) relies on specific dependencies (e.g. for webpack or babel).
What can also be done is you specify which versions those dependencies must resolve to, based on the error messages
This can be done using the resolutions field in package.json, e.g.:
"resolutions": {
"babel-loader": "8.1.0",
"webpack": "4.x.x",
}
After this all will work Fine.
2 Links to consider https://github.com/facebook/create-react-app/issues/10123
and https://github.com/storybookjs/storybook/issues/4764#issuecomment-740785850
Seems that most people are installing the package to get it to work even thou it says not to install in manually.
So try:
yarn add babel-loader#8.1.0

Duplicate Types error when using react, reactdom and react-redux types together

So I am trying to use react-redux with typescript. This is what my package.json looks like
"dependencies": {
"#types/react-dom": "15.5.0",
"#types/react": "15.0.4",
"#types/react-redux": "4.4.39",
"axios": "0.16.2",
"react": "15.0.0",
"react-dom": "15.0.0",
"redux": "3.6.0",
"react-redux": "5.0.6",
"redux-thunk": "2.1.0",
"office-ui-fabric-react": "1.0.0"
}
Now when I do yarn install. I see multiple react types get installed. Once for #types/React. One for #types/React-redux and #types/react-dom inside each of its own node_modules folder. And the version of the types these modules install internally are very different as I see in my yarn.lock file.
When I try to compile this, I get the errors like
error TS2304: Cannot find name 'DetailedHTMLFactory'.
Subsequent variable declarations must have the same type
The error disappears if I duplicate all the nested #types and just keep teh top level #types/react.
Whats a better way to do this, so that duplicate types issue doesn't appear ?
Since you are using yarn, the easiest direction might be to use resolutions in the package.json file to force specific versions of the typings, see https://github.com/yarnpkg/yarn/pull/4105
Other than that you would have to figure out which specific versions of these typings work with one another. Basically look at #types/react-dom dependency list and then include the same version of #types/react in your project.
You should try to remove your node_modules folder, run yarn cache clear and reinstall everything. There is an issue on the typescript repo about this behaviour ( I ran into it recently with one of my side projects ) that i'll try to find and link to this answer.
So I finally fixed this by excluding node_modules folder.
Put "node_modules" in the "exclude" section of tsconfig.json .

JSPM: Error locating github:component/global/archive/v2.0.1.tar.gz

I've spent the entire day trying to make jspm install a few libraries I'm required to as devDependencies. Unfortunately my frontend skills are not as good as my backend skills so that's why I'm asking for help.
I'm trying to add the browser-sync package, specifically version 2.7.13 (but it could be a newer one, I don't think it affects as it's a brand new codebase I need to set up). I've set up my package.json file this way:
{
"jspm": {
"dependencies": {
"angular": "github:angular/bower-angular#1.4.3",
"angular-animate": "github:angular/bower-angular-animate#1.4.3",
"angular-loading-bar": "github:chieffancypants/angular-loading-bar#0.8.0",
"angular-sanitize": "github:angular/bower-angular-sanitize#1.4.7",
"angular-ui-bootstrap": "npm:angular-ui-bootstrap#0.13.4",
"angular-ui-grid": "github:angular-ui/bower-ui-grid#3.0.6",
"angular-ui-router": "github:angular-ui/ui-router#0.2.15",
"bootstrap": "github:twbs/bootstrap#3.3.5",
"datatables": "github:DataTables/DataTables#1.10.9",
"jeet": "npm:jeet#6.1.2",
"jquery": "npm:jquery#2.1.4",
"lodash": "npm:lodash#3.10.0",
"normalize.css": "github:necolas/normalize.css#3.0.3",
"rupture": "npm:rupture#0.6.1"
},
"devDependencies": {
"angular-mocks": "npm:angular-mocks#^1.4.3",
"babel": "npm:babel-core#^5.8.24",
"browser-sync": "npm:browser-sync#^2.7.13",
"babel-runtime": "npm:babel-runtime#^5.8.24",
"core-js": "npm:core-js#^1.1.4"
}
}
}
Then I run jspm update (or jspm install npm:browser-sync) and it throws:
err Error locating github:component/global/archive/v2.0.1.tar.gz.
I have no idea how to solve this, honestly. Google doesn't throw much results so I'm practically blind.
I also have to add these packages (compatible with Angular 1.4.3 or something), but I'm trying to add them one by one now as adding them as a whole gave me a whole lot of errors which required me to input my github credentials, but it made no difference:
angular-mocks
babel-loader
browser-sync
chai
css-loader
file-loader
gulp
gulp-rename
gulp-template
gulp-todoist
http-backend-proxy
json-loader
jspm
karma
karma-chai
karma-chrome-launcher
karma-mocha
karma-mocha-reporter
karma-sourcemap-loader
mocha
ng-mock-e2e
node-libs-browser
raw-loader
run-sequence
style-loader
stylus-loader
yargs
Any help is greatly appreciated!
The issue come from an socket.io dependency updated in the jspm registry.
Now you should install it from github :
jspm install github:browsersync/browser-sync
and you can force the version, for eg.
jspm install github:browsersync/browser-sync#2.7.13

Resources