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

7

9

I'm currently using Create-React-App for my personal site. I keep getting the following errors every time I run it:

./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

I definitely feel like I'm missing something super small but I'm having trouble figuring it out. I tried Googling the error keyword 'import/first' and it's leading me to think it's an ESLint issue. Please let me know if you see any problem in my import order. I've tried different import orders, but nothing seems to get rid of the error.

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();
Disclose answered 9/8, 2017 at 21:13 Comment(0)
K
17

This is because you accidentally installed React Router into src folder. So the linter thinks it’s your code and tries to validate it. Don’t install third party dependencies inside src.

Delete src/node_modules and run npm install in the root folder of your app. If some package is missing, run npm install --save <package-name>, also in the root folder.

Kenspeckle answered 10/8, 2017 at 10:55 Comment(0)
R
11

For me, it was a case, because I was violating Eslint import/first rule, by calling an imported function before any other import.

For example, this code had a problem:

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

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

because I was calling and assigning const store = configureStore(); before import 'isomorphic-fetch';

Rana answered 23/8, 2017 at 10:18 Comment(0)
V
7

The import declaration is incorrect we need to follow the procedure such

1) first we need to import library

ex: import React from 'react';

2) Then declare any variable or constants

ex: var apiBaseUrl = "http://localhost:4000/api/";

Veil answered 16/5, 2018 at 21:6 Comment(1)
For me it was a simple console.log() between two imports XDSalvadorsalvadore
B
3

Look closely at your code. I saw this message from a double ; typo.

      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';

Batista answered 31/1, 2019 at 18:8 Comment(0)
E
2

If you're here because you were using React.lazy to lazy load a component, you must specify all your import statements before any React.lazy() lines. Another way of saying this is, you cannot have any import statements after your lazy loaded components.

See example for order

import Footer from "./components/Footer.js";
const Header = React.lazy(() => import("components/Header"));
Examine answered 30/7, 2020 at 23:37 Comment(0)
I
1

In my case, I got this error for the below piece of code. Before fix:-

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

After spending some time on this, I am able to find cause for this." all import statements should be at the top of the module,"

After fix:-

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

export const GET_TODO = 'GET TODO';
export const SAVE_TODO = 'SAVE TODO';
Inhume answered 13/5, 2019 at 9:18 Comment(0)
J
0

My problem was that I had at the second line this

var moment = require('moment');

All the other lines were imports. When I moved the require to the end, problem fixed.

Jsandye answered 20/8, 2019 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.