react-md-editor: Does not work with NextJS
NextJS does not allow global CSS to be imported at the component level, only in _app.js.
error - ./node_modules/@uiw/react-md-editor/lib/esm/index.css
Global CSS cannot be imported from within node_modules.
Read more: https://err.sh/next.js/css-npm
See https://codesandbox.io/s/compassionate-cookies-msmsd?file=/pages/index.js
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 20
- Comments: 59 (15 by maintainers)
Commits related to this issue
- fix: fix #52 by exporting an unstyled version of the components — committed to samipourquoi/react-md-editor by samipourquoi 3 years ago
- fix: fix #52 by exporting an unstyled version of the components — committed to samipourquoi/react-md-editor by samipourquoi 3 years ago
- doc: Update README.md #52 — committed to uiwjs/react-md-editor by jaywcjlove 3 years ago
- doc: Update README.md #52 — committed to uiwjs/react-md-editor by jaywcjlove 3 years ago
- doc: Update README.md #52 — committed to uiwjs/react-md-editor by jaywcjlove 3 years ago
- [UI] @uiw/react-md-editor NextJS 오류 대처 https://github.com/uiwjs/react-md-editor/issues/52 — committed to woogieReal/MDGround3 by woogieReal 2 years ago
I created a nextjs package
next-remove-importsto solve the problem.Example: https://codesandbox.io/s/nextjs-example-react-md-editor-qjhn7?file=/pages/index.js
Example: @uiwjs/next-remove-imports/example
Example: https://next-remove-imports-example.vercel.app
@rafaelguedes @serendipity1004 @karlhorky @namnd @A7U @stLoiz @veksen @earthnoob @jdudmesh @diego9497
example: https://codesandbox.io/s/nextjs-example-react-md-editor-forked-89jwvb?file=/pages/index.js
Solution to >
error - ./node_modules/@uiw/react-md-editor/lib/esm/index.css Global CSS cannot be imported from within node_modules. Read more: https://err.sh/next.js/css-npmIn your next.js component or page, add this>
import '@uiw/react-md-editor/dist/markdown-editor.css'import '@uiw/react-md-editor/lib/esm/components/DragBar/index.css'import '@uiw/react-md-editor/lib/esm/components/TextArea/index.css'import '@uiw/react-md-editor/lib/esm/components/Toolbar/index.css'import '@uiw/react-md-editor/lib/esm/components/Toolbar/Child.css'Then, go to
node_modules/@uiw/and comment all imports of.cssfiles. It will start working. It worked for me onnext 10.0.7😃@jaywcjlove Can you just remove the imports from your package, and ask us to import it ourselves? Your solution with
next-remove-importsis not working for me.Yes. It’s almost impossible to suit all the needs of so many frameworks these days and I thank you so much for taking time and effort to build a very beautiful open source. However, if you want your package to grow (I don’t know if this is your intention) it would be a good idea to encapsulate NextJS users because it is one of the biggest SSR framework. I don’t think this is something you should be unhappy about. You should be happy because it just means that your package is gaining a lot of popularity. That’s why there are demands here and there.
For those struggling, I was able to fix this issue without installing
next-remove-importsby adding the linetranspilePackages: ['react-md-editor']to mynext.config.jsfile.I was having the exact problem and managed to fix by installing
@zeit/next-cssand updatednext.config.js:Hope that helps you, let me know if it works.
solutions are 👍
install :- npm install next-remove-imports npm install @uiw/react-md-editor@v3.6.0
// next.config.js file write :- const removeImports = require(‘next-remove-imports’)(); module.exports = removeImports({});
//File :- import “@uiw/react-md-editor/markdown-editor.css”; import “@uiw/react-markdown-preview/markdown.css”; import dynamic from “next/dynamic”; import { useState } from “react”;
const MDEditor = dynamic( () => import(“@uiw/react-md-editor”), { ssr: false } );
function Pages() { const [value, setValue] = useState(“Hello world!!!”); return ( <div> <MDEditor value={value} onChange={setValue} /> </div> ); } export default Pages;
I used the plugin but I’m still encountering an error. The editor/preview seems to work fine when I navigate to the page with it via
<Link />, however if I refresh that page I get the following error:Given the snippet, how would one go about import
commandsandmarkdownUtils(such as selectWord)? The default exported component works but I can’t seem to figure out how to import anything else. Attempting to import anything else gives this as an error.@matteoturini The example still works: https://codesandbox.io/s/nextjs-example-react-md-editor-qjhn7?file=/pages/index.js
But it’s not a good, long-term, robust solution.
@jaywcjlove I would again recommend switching to get users to import the styles themselves: https://github.com/uiwjs/react-md-editor/issues/52#issuecomment-805623260
1.Create a
/static folderat the same level the/pagesfolder. 2. In that folder put your.cssfiles 3. In your page components importHeadand add a<link />to your CSS.Reference: https://github.com/vercel/next.js/issues/299#issuecomment-263146962
As @preetpatel mentioned the only thing you have to do is use:
Here I leave the code that I used in a NextJS 13.0.7 App:
next.config.js
page.tsx (Component)
As you can see there is no need to install additional packages like next-remove-imports or use specific NextJS imports for the component.
What worked for me (Next.js version 13.4.3) is inside your next.config.mjs add the following to your config:
For anyone is still looking for a temporary solution, I’ve created a forked package out of this one https://www.npmjs.com/package/@namskiiiii/react-md-editor-naked just to remove the styling
The suggested solution is now deprecated by Zeit/Vercel. Consider a proper fix and not import the CSS in the node_modules 😃
i got types error with typescript .
I just wanted to drop a quick heads up on style imports! it looks like they are not bundled with this name any more:
instead, they are:
here is a screenshot of dist folder inside of node-modules that got me for 2 mins to figure it out:
Just to consolidate the solution: Yes, the current solution works! Even so I kept getting errors, but the reason was simply the wrong use of the “next-remove-imports” plugin, when I needed to export more than one module in next.config.js . I was forgetting to put “options” as argument to “next-remove-imports”. I will put a clean and simple code for understanding.
Wrong mode:
Correct mode:
I agree with @serendipity1004. NextJS is one of the biggest SSR frameworks and I believe that not encapsulating its users will prevent this amazing package to grow. I’m also trying to make this work with NextJS and I’m struggling. Can you please merge the PR #131 assuming that it will fix this issue? @jaywcjlove
The problem is that MDEditor.tsx has an import for index.less. The component will only work with NextJS if this import is removed. This requires changing react-md-editor. Consider moving the CSS for the component into a separate file which can be imported independently i.e. in _app.js.