Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
MrPedrinho opened this issue Aug 18, 2021 · 14 comments
Closed

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

MrPedrinho opened this issue Aug 18, 2021 · 14 comments

Comments

@MrPedrinho
Copy link

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
Copy link
Member

@jaywcjlove
Copy link
Member

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

@BobinBieber

@jaywcjlove
Copy link
Member

I'm having problems with ESM and Next.js

You must enable the experimental support for ESM.

@BobinBieber

@jaywcjlove
Copy link
Member

jaywcjlove commented Aug 18, 2021

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',
        ]
    }
})

@MrPedrinho
Copy link
Author

@jaywcjlove I still get the same error

@jaywcjlove
Copy link
Member

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

@jaywcjlove
Copy link
Member

@BobinBieber vercel/next.js#27876

@killaslark
Copy link

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
Copy link
Member

#52 (comment)

@jaywcjlove
Copy link
Member

jaywcjlove commented Aug 28, 2021

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;

#52 (comment)

@bear-bear-bear
Copy link

bear-bear-bear commented Sep 23, 2021

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
Copy link

Altabeh commented Feb 4, 2022

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants