javascript - 错误 : Must use import to load ES Module: D:\node_modules\react-markdown\index. 不支持 ES 模块的 js require()

标签 javascript reactjs npm markdown strapi

目前我正在使用"react": "17.0.2"我已经安装了"react-markdown": "^7.0.1"通过 npm i react-markdown我正在使用 this package显示我从 Strapi CMS 获取的富文本。我使用以下代码来显示内容:

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 },
  };
}
但是当我运行它时,它给了我以下错误:
enter image description here
我正在使用节点 v14.17.0并尝试添加 "type": "module" .
我的 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"
  }
}

最佳答案

我通过将 react-markdown 固定到版本 6 来解决这个问题。
使用 react-markdown 的第 7 版,他们只迁移到 ESM。有一个解释是什么意思here ,但似乎 Jest(版本 27)仍然只有 experimental support对于截至 2021 年 10 月的纯 ESM 模块。我无法让它在我的项目中工作,并且 react-markdown 版本 6 目前可以正常工作。
我认为 Jest 的下一个主要版本(版本 28)可能支持 ESM。

关于javascript - 错误 : Must use import to load ES Module: D:\node_modules\react-markdown\index. 不支持 ES 模块的 js require(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69008215/

相关文章:

javascript - 为什么这个 JSON 文件读起来很奇怪

javascript - 我怎样才能逃避 React useEffect 无限循环?

javascript - 如何在运行 karma 时修复 "There is no server listening on port 9876"错误?

node.js - Protractor 错误 - @types/jasmine' 不在 npm 注册表中

ubuntu - Jenkins 突然显示 "wrapper script does not seem to be touching the log file"并超时并显示 FAILURE 消息

javascript - Sage 和 Google 图表中的纵横比

javascript - 由于结果不一致,可以替代 document.createElement 来触发像素(请求图像)

javascript - 当我们选中和取消选中其他表中的复选框时,如何在 jquery 中的表中添加/删除行?

reactjs - 如何在 React Router v4 中使用 context api?

reactjs - 如何在 Draft.js 中创建自定义键绑定(bind)?