error - unhandledRejection: Error: Not supported at Object.react-markdown - reactjs

Currently I'm using react-markdown as my markdown component in my react project. Along with this I'm also using rehype-raw and remark-gfm. Whenever I run the project I get the following error:
The following is my package.json:
"dependencies": {
"next": "11.1.2",
"next-images": "^1.8.1",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-markdown": "^7.0.1",
"rehype-raw": "^6.1.0",
"remark-gfm": "^2.0.0",
},
and my component:
import ReactMarkdown from "react-markdown";
import styles from "../styles/Home.module.css";
import remarkGfm from "remark-gfm";
import rehypeRaw from "rehype-raw";
export default function Home({ posts }) {
return (
<main className={styles.main}>
{posts &&
posts.map((image) => (
<div style={{ height: "100%" }}>
<ReactMarkdown
children={image.Content}
rehypePlugins={[rehypeRaw]}
/>
</div>
))}
</main>
);
}
Error shown in logs:

I experienced similar error too, then I've found this solution:
https://github.com/vercel/next.js/issues/25454#issuecomment-903513941
ReactMarkdown is an ESM module which means it cannot be required but rather imported. If you are actually importing in your code, it can be, that the transpiler (e.g.: Babel) changes this, hence the error. So this can be a possible cause of the error.
What you can do to solve this (also described in the issue commit by others):
npm i next-transpile-modules
In next.config.js do the following:
const withTM = require('next-transpile-modules')(['react-markdown']);
module.exports = withTM({
...restOfYourConfig
})
Import ReactMarkdown like this: import ReactMarkdown from 'react-markdown/react-markdown.min';
Other sources:
https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
https://nextjs.org/docs/messages/import-esm-externals
https://www.npmjs.com/package/next-transpile-modules

Related

React v18 and react-scripts v5 throwing a random error

Here is a short background...
I migrated react from v17 to v18 successfully. App is running without errors.
After that I upgraded react-scripts from v4 to v5. There are no errors in the terminal but when i run the app, it's crashed (white screen). There is a strange error in console Uncaught TypeError: Cannot read properties of undefined (reading 'default')
Here is a screenshoot of the actual error:
This is the file where the above error is thrown:
import { Outlet } from 'react-router-dom';
import DashboardNavbar from '../DashboardNavbar';
import DashboardSidebar from '../DashboardSidebar';
import { StyledRoot, StyledContainer } from './DashboardLayout.styles';
const DashboardLayout = () => (
<StyledRoot>
<DashboardNavbar />
<DashboardSidebar />
<StyledContainer>
<Outlet />
</StyledContainer>
</StyledRoot>
);
export default DashboardLayout;
Dependencies from package.json
{
...
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "^5.0.1",
"typescript": "^4.8.4",
...
}
Do you have any idea why is this happening?

Nextjs - Dynamic Imports - CSS Modules cannot be imported from within node_modules

Struggling to wrap my head around this issue. I am using the built in CSS modules for my components in Nextjs. When i lazy load my component that has a CSS module, I get the error CSS Modules cannot be imported from within node_modules..
If I replace ${asset.name} with the value button, i.e dynamic(() => import(#preamp/assets/Button)), nextjs will compile.
The code will execute correctly if i dont use CSS modules. It will lazy load dynamic components in. Only when i add the file to the project.
If i hardcode the import path it compiles. I only have this issue when I use a template literal string.
Any help is highly appreciated.
import React from 'react';
import dynamic from 'next/dynamic';
const asset = { name: 'Button' };
const NewComponent = dynamic(() => import(`~/assets/${asset.name}`), {
ssr: false,
});
export default function index() {
return (
<div className="grid-container">
<div className="grid-x grid-margin-x">
<div className="cell medium-6 large-6">
<NewComponent />
</div>
<div className="cell medium-6 large-6">12/6/8 cells</div>
</div>
</div>
);
}
Button.js
import React from 'react';
import styles from './Test.module.css';
export default function Index() {
return <div className={styles['btn-primary']}>Test Div</div>;
}
EDIT:
If I move the Test.module.css into a different folder, it will compile. I havent seen any documentation or reasoning why the CSS modules has to live in a particular area.
button.js updated
import React from 'react';
import styles from '~/test/Test.module.css'; // <-- Moved into a different folder
export default function Index() {
return <div className={styles['btn-primary']}>Test Div</div>;
}
Project specs:
"dependencies": {
"classnames": "^2.3.1",
"eslint-plugin-prettier": "^3.4.1",
"eslint-plugin-react": "^7.27.0",
"next": "^12.0.3",
"next-transpile-modules": "^9.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"sass": "^1.43.4"
},
Using node v14.17.3
Just wanted to post my answer. If you are going to use dynamic imports, then import paths themselves cannot be dynamic. I've added more information here.
https://github.com/vercel/next.js/issues/31271

Invalid Hook call on using material UI in react

when I tried to use matrial UI textfield I got the invalid hook call error. how to fix it ?
import './Login.css';
import TextField from "#material-ui/core/TextField";
const Login = () => {
return(
<div className="login">
<div className="loginContainer">
//***** <TextField /> *****//
</div>
</div>
);
}
export default Login;
When I remove the TextField Component it works fine. when I add it it says
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
I have the react and react-dom same versions
"react": "^17.0.2",
"react-dom": "^17.0.2"
but still I got the error, can anyone give me the reason why it is so ?
I have react and react-dom versions 17.0.2 in package.json before
Then I changed it to
{
"#material-ui/core": "latest",
"react": "latest",
"react-dom": "latest"
}
and then I executed the "npm install" It installed the latest versions of all the dependencies mentioned above. This fixed the issue.

Module not found: Can't resolve 'react-bootstrap/Navbar'

I keep getting this error despite having tried to install react-bootstrap, reactstrap, adding the scripts and imports from react-bootstrap documentation.
I have tried : npm i react-bootstrap --save
And: npm install react-bootstrap bootstrap
I have created a similar project with this same component and it works perfectly fine, I don't understand why it won't work in this project.
This is the component file:
import React from 'react';
import './Header.css'
import Navbar from 'react-bootstrap/Navbar'
import {Container} from "reactstrap";
import 'bootstrap/dist/css/bootstrap.min.css';
import { NavLink } from 'react-router-dom';
const Navigation = () =>{
return(
<div className="header">
<Navbar bg="dark" variant="dark" expand="lg">
<Container>
<Navbar.Brand href="#home"><NavLink to="/">Home</NavLink></Navbar.Brand>
<Navbar.Brand href="#about"><NavLink to="/catallaxy">Catallaxy</NavLink></Navbar.Brand>
<Navbar.Brand href="#home"><NavLink to="/events">Events</NavLink></Navbar.Brand>
<Navbar.Brand href="#about"><NavLink to="/about">About Us</NavLink></Navbar.Brand>
<Navbar.Brand href="#contact"><NavLink to="/contact">Gallery</NavLink></Navbar.Brand>
</Container>
</Navbar>
</div>
)
}
export default Navigation;
I wanted to put this as a comment, as it is a simple and quick solution, but code cannot be formatted there.
import React from 'react';
import './Header.css'
import {Container, Navbar } from "reactstrap"; //this line here is the solution.
import 'bootstrap/dist/css/bootstrap.min.css';
import { NavLink } from 'react-router-dom';
and your package.json should have reactstrap like this.
"dependencies": {
"classnames": "2.2.6",
"moment": "2.26.0",
"node-sass": "4.14.1",
"nouislider": "14.5.0",
"react": "16.13.1",
"react-bootstrap-switch": "15.5.3",
"react-datetime": "2.16.3",
"react-dom": "16.13.1",
"react-router": "5.2.0",
"react-router-dom": "5.2.0",
"react-scripts": "3.4.1",
"reactstrap": "8.4.1". <------- here
}
this is what usually works. here is a reference link of a working navbar - https://github.com/Jay-study-nildana/RandomStuffReactJSApp/blob/master/src/components/Navbars/IndexNavbar.js
react-bootstrap and reactstrap are almost same things and they provide same components.
Just use one of them and import both Navbar and Container.
import {Container, Navbar} from "react-bootstrap"
I was facing same issue
Solution:
import { Container, Navbar } from "react-bootstrap";
Note: Make sure you already installed the below package:
npm i --save react-bootstrap
Result:
npm i --save react-bootstrap
I am facing error in installing this package

react material-ui textfield getting error: invalid hook call

I have this simple code to show the text field:
I get the error
Error in /turbo_modules/react#16.13.0/cjs/react.development.js (1465:13)
Invalid hook call.
The code is at this link https://stackblitz.com/edit/react-6dgvfj?file=UserForm.js
import React, { Component } from 'react';
import compose from 'recompose/compose';
import { connect } from 'react-redux';
import TextField from '#material-ui/core/TextField';
const UserForm = props => {
return (
<div>
This is userform component.
<TextField id="standard-basic" label="Standard" />
</div>
);
}
export default UserForm;
react and react-dom packages always get released in pair. So they are bound to be of same release, to work together. In your case, react was of 16.12.0 and react-dom of 16.13.0
Your package.json
"dependencies": {
"#material-ui/core": "^4.9.5",
"#types/react": "^16.8.6",
"react": "^16.13.0",
"react-dom": "16.12.0"
},
change it to
"react": "16.13.0",
"react-dom": "16.13.0"

Resources