CRA + React Leaflet:编译失败

Vic*_*ara 9 react-leaflet create-react-app

我刚刚开始了一个全新的项目,使用create-react-appreact-leaflet按照他们的文档在这里推荐的进行设置。

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

./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;
Run Code Online (Sandbox Code Playgroud)

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

Vid*_*rma 9

After reading about it on all blogs , I have concluded that : It's because of the new version of the react-leaflet. I faced the same problem and here's how I got rid of the error:

Open your package.json file

 "browserslist": {
 "production": [
  ">0.2%",
  "not dead",
  "not op_mini all"
],
"development": [
  "last 1 chrome version",
  "last 1 firefox version",
  "last 1 safari version"
]
},
Run Code Online (Sandbox Code Playgroud)

Replace it with following lines :

"browserslist": [
">0.2%",
"not dead",
"not op_mini all"
],
Run Code Online (Sandbox Code Playgroud)
  • Now Delete node_modeules/.cache folder

  • npm install

  • npm start

Another method is to just add these lines in your package.json file:

"react-leaflet": ">=3.1.0 <3.2.0 || ^3.2.1",
"@react-leaflet/core": ">=1.0.0 <1.1.0 || ^1.1.1"
Run Code Online (Sandbox Code Playgroud)

  • 如果您是视觉学习者,这里有一个具有相同解决方案的视频。https://youtu.be/tFqj-JKYr4M (6认同)