Create-React-App 编译失败 | Import/First 错误

Create-React-App failed to compile | Import/First Error

我目前正在为我的个人网站使用 Create-React-App。每次 运行 时,我都会收到以下错误:

./src/~/react-router-dom/es/index.js
 Line 3:   Import in body of module; reorder to top  import/first
 Line 5:   Import in body of module; reorder to top  import/first
 Line 7:   Import in body of module; reorder to top  import/first
 Line 9:   Import in body of module; reorder to top  import/first
 Line 11:  Import in body of module; reorder to top  import/first
 Line 13:  Import in body of module; reorder to top  import/first
 Line 15:  Import in body of module; reorder to top  import/first
 Line 17:  Import in body of module; reorder to top  import/first
 Line 19:  Import in body of module; reorder to top  import/first
 Line 21:  Import in body of module; reorder to top  import/first
 Line 23:  Import in body of module; reorder to top  import/first
 Line 25:  Import in body of module; reorder to top  import/first

我确实觉得我遗漏了一些非常小的东西,但我很难弄明白。我尝试使用谷歌搜索错误关键字 'import/first',这让我认为这是一个 ESLint 问题。如果您发现我的进口订单有任何问题,请告诉我。我尝试了不同的进口订单,但似乎无法消除错误。

import React from 'react';
import ReactDOM from 'react-dom';
import { createBrowserHistory } from 'history';
import { Router, Route, Redirect, Switch } from 'react-router-dom';
import './index.css'; 
import App from './App.js';
import Home from './home.js';
import About from './about.js';
import Contact from './contact.js';
import NotFound from './404.js';

import registerServiceWorker from './registerServiceWorker';

const history = createBrowserHistory();

ReactDOM.render(
    <Router history={history}>
        <App>
            <Switch>
                <Route exact path="/" component= {Home} />
                <Route path="/about" component= {About} />
                <Route path="/contact" component= {Contact} />
                <Route path="/404" component= {NotFound} />
                <Redirect to= "/404" />
            </Switch>
        </App>
    </Router>,
    document.getElementById('root')
);
registerServiceWorker();

这是因为您不小心将 React Router 安装到了 src 文件夹中。所以 linter 认为它是 你的 代码并尝试验证它。 不要在src里面安装第三方依赖。

删除应用程序根文件夹中的 src/node_modules 和 运行 npm install。如果缺少某些包,运行 npm install --save <package-name>也在根文件夹中

对我来说,这是一个案例,因为我违反了 Eslint import/first 规则,在任何其他导入之前调用导入函数。

例如,这段代码有问题:

import configureStore from './redux/common/configureStore';
const store = configureStore();

// Add polyfill for fetch for older browsers
import 'isomorphic-fetch';
require('es6-promise').polyfill();

因为我在 import 'isomorphic-fetch';

之前调用并分配了 const store = configureStore();

进口申报不正确,我们需要按照

这样的程序

1) 首先我们需要导入库

例如:import React from 'react';

2) 然后声明任何变量或常量

例如:var apiBaseUrl = "http://localhost:4000/api/";

仔细查看您的代码。我从双 ; 打字错误中看到这条消息。

      import React from 'react';
      import AppBar from '@material-ui/core/AppBar';
      import CircularProgress from '@material-ui/core/CircularProgress';;  <----- Mistake
      import Toolbar from '@material-ui/core/Toolbar';
      import IconButton from '@material-ui/core/IconButton';

在我的例子中,我在下面的代码中遇到了这个错误。 修复前:-

import axios from 'axios';
export const GET_TODO = 'GET TODO';
export const SAVE_TODO = 'SAVE TODO';
import { devConstants } from 'src/appConstants';

在这方面花了一些时间后,我找到了原因。"所有导入语句 应该位于模块的顶部,"

修复后:-

import axios from 'axios';
import { devConstants } from 'src/appConstants';

export const GET_TODO = 'GET TODO';
export const SAVE_TODO = 'SAVE TODO';

我的问题是我在第二行有这个

var moment = require('moment');

所有其他行都是进口的。当我将要求移到最后时,问题就解决了。

如果您来到这里是因为您使用 React.lazy 延迟加载组件,则必须在任何 React.lazy() 行之前指定所有导入语句。另一种说法是,在延迟加载的组件之后不能有任何 import 语句。

查看订单示例

import Footer from "./components/Footer.js";
const Header = React.lazy(() => import("components/Header"));