'Module parse failed' react-scripts 3.0.0 更新后延迟加载时出错

'Module parse failed' error while lazy loading after react-scripts 3.0.0 update

我在我的 React 应用程序中延迟加载组件,并且一切正常。但是,在 react-scripts 3.0.0 更新后,我不断收到如下 'Module Parse failed: Unexpected token' 错误:

./src/routes/App.js 22:9
Module parse failed: Unexpected token (22:9)
You may need an appropriate loader to handle this file type.
| import { Loader } from '../components/Preloaders/Loader';
| var ClippedDrawer = lazy(function () {
>   return import('../components/Drawer');
| });
| var Settings = lazy(function () {

这是文件的开头 App.js:

import React, { lazy, Suspense } from 'react';
import {
    Router,
    Route,
    Switch
} from 'react-router-dom';

import { Loader } from '../components/Preloaders/Loader';

const ClippedDrawer = lazy(() => import('../components/Drawer'));
const Settings = lazy(() => import('../containers/Settings/Settings'));
const NotFound = lazy(() => import('../containers/NotFound'));

这有什么问题,我该如何解决?

找到解决方案:(但是,请备份您的项目以防失败)。

  • 删除node_modules
  • 删除package-lock.json package.json

在终端中:

  • 运行 npm install react-scripts@latest
  • 运行 npm install

访问 https://github.com/facebook/create-react-app/issues/6673 了解更多详情。