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

ESM package cannot be imported #466

Open
zhangela opened this issue Dec 23, 2022 · 1 comment
Open

ESM package cannot be imported #466

zhangela opened this issue Dec 23, 2022 · 1 comment

Comments

@zhangela
Copy link

Getting this error when I followed the docs:

./node_modules/@uiw/react-markdown-preview/lib/index.js:13:0
Module not found: ESM packages (react-markdown) need to be imported. Use 'import' to reference the package instead. https://nextjs.org/docs/messages/import-esm-externals

Import trace for requested module:
./app/page.tsx

https://nextjs.org/docs/messages/module-not-found

Looks like the issue is uiwjs/react-md-editor is CommonJS but react-markdown is ESM, so this line in react-markdown-preview/lib/index.js causes the error above:

var _reactMarkdown = _interopRequireDefault(require("react-markdown"));

Here's what I have:

next.config.js

/** @type {import('next').NextConfig} */
require("dotenv").config();

const nextConfig = {
  reactStrictMode: true,
  swcMinify: true,
  experimental: {
    appDir: true,
    esmExternals: true,
  },
};

const removeImports = require("next-remove-imports")();

module.exports = removeImports(nextConfig);

app/page.tsx:

"use client";

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").then((mod) => mod.default),
  { ssr: false }
);
const EditerMarkdown = dynamic(
  () =>
    import("@uiw/react-md-editor").then((mod) => {
      return mod.default.Markdown;
    }),
  { ssr: false }
);
const Markdown = dynamic(
  () => import("@uiw/react-markdown-preview").then((mod) => mod.default),
  { ssr: false }
);

function HomePage() {
  const [value, setValue] = useState("**Hello world!!!**");
  return (
    <div data-color-mode="dark">
      <MDEditor value={value} onChange={() => {}} />
      <div style={{ paddingTop: 50 }}>
        <Markdown source={value} />
      </div>
      <EditerMarkdown source={value} />
    </div>
  );
}

export default HomePage;
@jaywcjlove
Copy link
Member

@zhangela #52 (comment)

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

2 participants