无法让 antd 与 create-react-app 一起工作

Can't get antd to work with create-react-app

我想尝试将 ant.design 与 create-react-app 一起使用,所以我按照 antd 文档中给出的步骤进行操作。

https://ant.design/docs/react/use-with-create-react-app

npx create-react-app antd-demo
npm add antd

在app.js文件中,

import React from 'react';
import { Button } from 'antd';
import './App.css';

const App = () => (
  <div className="App">
    <Button type="primary">Button</Button>
  </div>
);

export default App;

并在 App.css

@import '~antd/dist/antd.css';

.App {
  text-align: center;
}

...

当我启动它时,出现了这个错误

错误:找不到模块“./locale”

▶ 折叠了 2 个堆栈框架。

https://imgur.com/a/0Yc4AdM (报错截图)

所以我做了 google 它,我发现它与我安装的 moment.js 有关,但我仍然继续遇到同样的错误。

您可以尝试Moment具体版本按照步骤操作。 Moment 版本 2.25.1 将不起作用。如何使用 npm 而不是 yarn 和 resolutions 块强制使用 2.24.0 版本?

简单技巧:-

npm install --save-dev npm-force-resolutions
add in package.json
 "resolutions": {
    "moment": "2.24.0"
 }

添加到 package.json

中的 "scripts" 节点
"preinstall": "npx npm-force-resolutions"

输入 npm install

希望这对您有所帮助。 有关详细信息,请按照 Bug (https://github.com/ant-design/ant-design-pro/issues/6512) 记录在 Antd 团队中。