uiwjs / react-md-editor

A simple markdown editor with preview, implemented with React.js and TypeScript.
https://uiwjs.github.io/react-md-editor
MIT License
2.17k stars 157 forks source link

Using next-remove-imports but still can't deploy - Next.js #224

Closed PedroLopes65777 closed 2 years ago

PedroLopes65777 commented 3 years ago

When I try to deploy on Vercel with Next.js I always get this error

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /vercel/path0/node_modules/react-markdown/index.js

I already checked #52, used next-remove-imports but this happens. This is my next.config.js

const removeImports = require('next-remove-imports')({})

module.exports = removeImports({
    images: {
        domains: [
            'cdn.discordapp.com',
            'discordapp.com',
        ]
    }
})

And this how I'm importing react-md-editor

import MDEditor from '@uiw/react-md-editor';
import '@uiw/react-md-editor/dist/markdown-editor.css'
import '@uiw/react-markdown-preview/dist/markdown.css';
jaywcjlove commented 3 years ago

@BobinBieber https://codesandbox.io/s/nextjs-example-react-md-editor-qjhn7?file=/pages/index.js

Use ESM (please read this)

jaywcjlove commented 3 years ago

Or before using the @uiw/react-md-editor@3.4.7 version.

@BobinBieber

jaywcjlove commented 3 years ago

I'm having problems with ESM and Next.js

You must enable the experimental support for ESM.

@BobinBieber

jaywcjlove commented 3 years ago

We're working on extensive ES Modules support in Next.js, both as input modules and as an output target. Starting with Next.js 11.1, you can now import npm packages using ES Modules (e.g. "type": "module" in their package.json) with an experimental flag.

// next.config.js
module.exports = {
  // Prefer loading of ES Modules over CommonJS
  experimental: { esmExternals: true }
}
const removeImports = require('next-remove-imports')({})

module.exports = removeImports({
    // Prefer loading of ES Modules over CommonJS
    experimental: { esmExternals: true },
    images: {
        domains: [
            'cdn.discordapp.com',
            'discordapp.com',
        ]
    }
})
PedroLopes65777 commented 3 years ago

@jaywcjlove I still get the same error

jaywcjlove commented 3 years ago

@BobinBieber This error should go to Next.js to study it.

jaywcjlove commented 3 years ago

@BobinBieber https://github.com/vercel/next.js/discussions/27876

killaslark commented 3 years ago

Hi @MrPedrinho

i got the same issue when i try to run it on dev mode

then i try to import it with dynamic from next/dynamic

import dynamic from 'next/dynamic'

const MDEditor = dynamic<MDEditorProps>(() => import('@uiw/react-md-editor').then(mod => mod.default), { ssr: false })

Then the error is gone. Maybe you can try this ?

jaywcjlove commented 3 years ago

Example: https://codesandbox.io/embed/nextjs-example-react-md-editor-qjhn7?fontsize=14&hidenavigation=1&theme=dark

jaywcjlove commented 3 years ago

https://github.com/uiwjs/react-md-editor/issues/52#issuecomment-848969341

jaywcjlove commented 3 years ago

Open in CodeSandbox Open in StackBlitz

npm install next-remove-imports
npm install @uiw/react-md-editor@v3.6.0
// next.config.js
const removeImports = require('next-remove-imports')();
module.exports = removeImports({});
import "@uiw/react-md-editor/markdown-editor.css";
import "@uiw/react-markdown-preview/markdown.css";
import dynamic from "next/dynamic";

const MDEditor = dynamic(
  () => import("@uiw/react-md-editor").then((mod) => mod.default),
  { ssr: false }
);

function HomePage() {
  return (
    <div>
      <MDEditor value="**Hello world!!!**" />
    </div>
  );
}

export default HomePage;

https://github.com/uiwjs/react-md-editor/issues/52#issuecomment-848969341

bear-bear-bear commented 3 years ago

This solution worked great for me. But when I tried to apply custom toolbar with commands property, I had the same problem. So I solved it like this:

before ❌

import { commands, MDEditorProps } from '@uiw/react-md-editor';

const MDEditor = dynamic<MDEditorProps>(() => import('@uiw/react-md-editor'), {
  ssr: false,
});

function HomePage() {
  return (
    <div>
      <MDEditor
         value="**Hello world!!!**"
         commands={[
           commands.bold,
           commands.italic,
         ]}
      />
    </div>
  );
}

export default HomePage;

after ⭕

import { MDEditorProps } from '@uiw/react-md-editor';
import { bold, italic } from '@uiw/react-md-editor/lib/commands';

const MDEditor = dynamic<MDEditorProps>(() => import('@uiw/react-md-editor'), {
  ssr: false,
});

function HomePage() {
  return (
    <div>
      <MDEditor
         value="**Hello world!!!**"
         commands={[
           bold,
           italic,
         ]}
      />
    </div>
  );
}

export default HomePage;
Altabeh commented 2 years ago

Note that if you want to use <MdEditor.Markdown /> to render some content with a source attribute, you should instead use the following code:

const MarkdownPreview = dynamic(
  () => import("@uiw/react-markdown-preview").then((mod) => mod.default),
  { ssr: false }
);
...

<MarkdownPreview source={content} className="mt-30" />
jaywcjlove commented 2 years ago

Example: @uiwjs/next-remove-imports/example