简体   繁体   English

错误:必须使用 import 加载 ES 模块:不支持 ES 模块的 D:\\node_modules\\react-markdown\\index.js require()

[英]Error: Must use import to load ES Module: D:\node_modules\react-markdown\index.js require() of ES modules is not supported

Currently I'm using "react": "17.0.2" and I have installed "react-markdown": "^7.0.1" via npm i react-markdown I'm using this package to display my rich text that I'm fetching from my Strapi CMS.目前我正在使用"react": "17.0.2"并且我已经通过 npm 安装了"react-markdown": "^7.0.1" npm i react-markdown我正在使用这个包来显示我的富文本m 从我的 Strapi CMS 中获取。 I have used the following code to display the content:我使用以下代码来显示内容:

import ReactMarkdown from "react-markdown";

export default function name({ posts }) {
  return (
    <>
      <div>
        <div>
          {posts.Title}
        </div>
      </div>
      <div>
        <ReactMarkdown source={posts.Content} escapeHtml={false} />
      </div>
    </>
  );
}

export async function getStaticProps() {
  const res = await fetch("http://localhost:1337/blogs");
  const posts = await res.json();

  return {
    props: { posts },
  };
}

But when I run this it gives me the following error:但是当我运行它时,它给了我以下错误:

在此处输入图像描述

I'm using node v14.17.0 and have tried adding "type": "module" .我正在使用节点v14.17.0并尝试添加"type": "module"

My package.json:我的 package.json:

{
  "name": "passportlegacy",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "axios": "^0.21.1",
    "babel-plugin-inline-react-svg": "^2.0.1",
    "next": "11.0.1",
    "next-images": "^1.8.1",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "react-map-gl": "^6.1.16",
    "react-markdown": "^7.0.1",
  },
  "devDependencies": {
    "@svgr/webpack": "^5.5.0",
    "@types/react": "17.0.16",
    "eslint": "7.32.0",
    "eslint-config-next": "11.0.1",
    "typescript": "4.3.5"
  }
}

I solved this by just pinning react-markdown to version 6.我通过将 react-markdown 固定到版本 6 来解决这个问题。

With version 7 of react-markdown they moved to ESM only.使用 react-markdown 的第 7 版,他们只迁移到 ESM。 There's a explanation of what that means here , but it seems Jest (version 27) still only has experimental support for pure ESM modules as of October 2021. I couldn't get it to work in my project, and version 6 of react-markdown works fine for now. 这里有一个关于这意味着什么的解释,但截至 2021 年 10 月,Jest(版本 27)似乎仍然只对纯 ESM 模块提供实验性支持。我无法让它在我的项目和 react-markdown 版本 6 中工作现在工作正常。

I think the next major version of Jest (version 28) will support ESM.我认为 Jest 的下一个主要版本(版本 28)将支持 ESM。

Node is currently treating your .js<\/code> file as CommonJS. Node 当前将您的.js<\/code>文件视为 CommonJS。 You need to tell Node to treat it as an ES module.您需要告诉 Node 将其视为 ES 模块。

Try adding "type": "module"<\/code> in your package.json<\/code> file.尝试在package.json<\/code>文件中添加"type": "module"<\/code> 。

You can place it anywhere at the top level.您可以将其放置在顶层的任何位置。 Eg:例如:

{
  "name": "passportlegacy",
  "version": "0.1.0",
  "type": "module",
  "private": true,
  "scripts": {
    ...
  }
  ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 [ERR_REQUIRE_ESM]:不支持来自 /app/commands/Image/meme.js 的 ES 模块 /app/node_modules/got/dist/source/index.js 的 require() - Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/got/dist/source/index.js from /app/commands/Image/meme.js not supported ES6 (ECMAScript 2015) 模块:导入 index.js - ES6 (ECMAScript 2015) modules: import index.js SSR with Node, Webpack, React, Express, Material UI, React Router - 错误 [ERR_REQUIRE_ESM]: 必须使用导入加载 ES 模块 - SSR with Node, Webpack, React, Express, Material UI, React Router - Error [ERR_REQUIRE_ESM]: Must use import to load ES Module 从 node_modules index.js 导入 class 定义 - import class definitions from node_modules index.js 不支持 ES 模块的 require() - require() of ES modules not supported React 应用程序的编译错误:模块构建失败(来自./node_modules/babel-loader/lib/index.js) - Compile error for a React app: Module build failed (from ./node_modules/babel-loader/lib/index.js) Nuxt.js:模块错误(来自./node_modules/eslint-loader/index.js): - Nuxt.js: Module Error (from ./node_modules/eslint-loader/index.js): 反应构建错误:模块构建失败(来自 ./node_modules/babel-loader/lib/index.js): - React build error: Module build failed (from ./node_modules/babel-loader/lib/index.js): Webpack esm 模块解析“警告 in../node_modules/react-bootstrap/esm/index.js” - Webpack esm module resolve “WARNING in ../node_modules/react-bootstrap/esm/index.js” 错误 in./src/index.js 模块构建失败(来自./node_modules/babel-loader/lib/index.js): - ERROR in ./src/index.js Module build failed (from ./node_modules/babel-loader/lib/index.js):
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM