gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:43:29 24 4
gpt4 key购买 nike

目前我正在使用"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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com