【问题标题】:CRA + React Leaflet: failed to compileCRA + React Leaflet:编译失败
【发布时间】:2021-08-05 16:00:10
【问题描述】:

我刚刚使用create-react-app 开始了一个全新的项目,并按照他们的文档推荐的here 设置了react-leaflet

我正在尝试使用this example 来检查它是否一切正常,但随后我收到以下错误:

./node_modules/@react-leaflet/core/esm/path.js 10:41
Module parse failed: Unexpected token (10:41)
File was processed with these loaders:
 * ./node_modules/react-scripts/node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|   useEffect(function updatePathOptions() {
|     if (props.pathOptions !== optionsRef.current) {
>       const options = props.pathOptions ?? {};
|       element.instance.setStyle(options);
|       optionsRef.current = options;

看起来react-scripts 无法处理react-leaflet 文件。 有人可以帮助我了解为什么会发生这种情况以及如何解决它吗?

【问题讨论】:

标签: create-react-app react-leaflet


【解决方案1】:

在阅读了所有博客上的相关内容后,我得出以下结论: 这是因为新版本的 react-leaflet。我遇到了同样的问题,这就是我摆脱错误的方法:

打开您的 package.json 文件

 "browserslist": {
 "production": [
  ">0.2%",
  "not dead",
  "not op_mini all"
],
"development": [
  "last 1 chrome version",
  "last 1 firefox version",
  "last 1 safari version"
]
},

用以下几行替换它:

"browserslist": [
">0.2%",
"not dead",
"not op_mini all"
],
  • 现在删除 node_modeules/.cache 文件夹

  • npm 安装

  • npm 开始

另一种方法是在你的 package.json 文件中添加这些行:

"react-leaflet": ">=3.1.0 <3.2.0 || ^3.2.1",
"@react-leaflet/core": ">=1.0.0 <1.1.0 || ^1.1.1"

【讨论】:

  • 第二种方法对我有用(没有尝试第一种)。确保按照说明删除缓存文件夹,但我还要说 package-lock.json
  • 如果您是视觉学习者,这里有一个具有相同修复的视频。 youtu.be/tFqj-JKYr4M
  • 视频正是我需要的,谢谢@Adam
  • 很高兴它有帮助。
猜你喜欢
  • 2021-03-27
  • 2021-06-14
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多