Attempted import error: 'Switch' is not exported from 'react-router-dom'

Reactjs

Reactjs Problem Overview


I don't know why I am getting this error and I can't find an answer for it anywhere. I have uninstalled the react-router-dom package and reinstalled it, but it continues to tell me that the switch module is not exported from react-router-dom. Here's my code.

The error I'm getting:

> Attempted import error: 'Switch' is not exported from 'react-router-dom'.

Code
import React from 'react';
import './App.css';
import NavBar from './components/navbar.js';
import Footer from './components/footer.js';
import Home from './components/pages/homepage/home.js';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';

function App() {
  return (
    <Router>
      <div className="app-container">
        <NavBar />
        <Switch>
          <Route path="/home" component={Home} />
        </Switch>
        <Footer />
      </div>
    </Router>
  );
}

export default App;

Reactjs Solutions


Solution 1 - Reactjs

In react-router-dom v6, "Switch" is replaced by routes "Routes". You need to update the import from

import { Switch, Route } from "react-router-dom";

to

import { Routes ,Route } from 'react-router-dom';

You also need to update the Route declaration from

<Route path="/" component={Home} />

to

<Route path='/welcome' element={<Home/>} />

In react-router-dom, you also do not need to use the exact in the Route declaration.

For more information, you can visit the official documentation: upgrade to react-router-dom v6

Solution 2 - Reactjs

If you are using react-router-dom v6 it looks like Switch has been replaced with Routes

Solution 3 - Reactjs

This is an example using react-router-dom V6

import React from 'react'
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'

import '../styles/global.css'

import Layout from '../containers/Layout'
import Home from '../pages/Home'
import Login from '../containers/Login'
import RecoveryPassword from '../containers/RecoveryPassword'
import NotFound from '../pages/NotFound'

const App = () => {
  return (
    <Router>
      <Layout>
        <Routes>
          <Route exact path="/" element={<Home/>}/>
          <Route exact path="/login" element={<Login/>}/>
          <Route exact path="/recovery-password" element={<RecoveryPassword/>}/>
          <Route path="*" element={<NotFound/>}/>
        </Routes>
      </Layout>
    </Router>
  );
}

export default App;

Solution 4 - Reactjs

I also faced the same problem, and I searched towards the Internet so much, but I didn't get any answer according to my question.

So I uninstalled the version 6 of react-router-dom:

npm uninstall react-router-dom

And installed version 5.2.0 of react-router-dom:

npm install react-router-dom@5.2.0

Solution 5 - Reactjs

Syntax has changed

Old Syntax

import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";

<Switch>
    <Route path="/home" component={Home} />
</Switch>

New Syntax:

import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom";

<Routes>
    <Route path="/home" element={<Home/>} />
</Routes>

Solution 6 - Reactjs

If you are using version 6 of react-router-dom, use this

Also, please see here for reference : https://reactrouter.com/docs/en/v6/upgrading/v5#:~:text=single%20route%20config%3A-,//%20This%20is%20a%20React%20Router%20v6%20app,%7D,-This%20step%20is

// This is a React Router v6 app
import {
  BrowserRouter,
  Routes,
  Route,
  Link,
  Outlet
} from "react-router-dom";

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="users" element={<Users />}>
          <Route path="me" element={<OwnUserProfile />} />
          <Route path=":id" element={<UserProfile />} />
        </Route>
      </Routes>
    </BrowserRouter>
  );
}

function Users() {
  return (
    <div>
      <nav>
        <Link to="me">My Profile</Link>
      </nav>

      <Outlet />
    </div>
  );
}

Solution 7 - Reactjs

In react-router-dom v6, Switch has been replaced with Routes. Use this format:

import { BrowserRouter as Router, Routes, Route} from 'react-router-dom';

<Router>
    <Routes>
        <Route exact path="/" component={component} />
        <Route exact path="/path2" component={component2} />
        <Route exact path="/path3" component={component3} />
    </Routes>
</Router>

Solution 8 - Reactjs

react-router-dom has updated to version 6. Now they have renamed the <Switch/> component to <Routes/>. There are also many changes.

You should spend sometime to read the documentation. Here is the link for react-router-v6-doc.

Solution 9 - Reactjs

I was able to fix it by changing from Switch to Routes So you should have something like this:

<Routes>
  <Route path='/home' element={<Home/>} />
</Routes>

and also you need to change the imort from Switch to Routes:

import { Routes, Route } from "react-router-dom";

Solution 10 - Reactjs

<Switch> is replaced by <Routes>

Before:

import { Route, Switch} from 'react-router'

<Router>
    <Switch>
        <Route />
        <Route />
    </Switch>
</Router>

Now:

import { Route, Routes} from 'react-router'

<Router>
    <Routes>
        <Route />
        <Route />
    </Routes>
</Router>

Just use Routes instead of Switch.

Solution 11 - Reactjs

Change from

import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";

<Switch>
    <Route path="/home" component={Home} />
</Switch>

to

import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom";

<Routes>
    <Route path="/home" element={ <Home />} />
</Routes>

Solution 12 - Reactjs

I solved my error by changing the way I was rendering my routes to the use of the element.

To:

import React from "react";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import { Home } from "./app";
import { Login, Register } from "./auth";
const R: React.FC = () => {
  return (
    <Router>
      <Routes>
        <Route path="/login" caseSensitive={false} element={<Login />} />
        <Route path="/register" caseSensitive={false} element={<Register />} />
        <Route path="/" caseSensitive={false} element={<Home />} />
      </Routes>
    </Router>
  );
};
export default R;

Basically before v6.*:

import React from "react";
import { BrowserRouter as Router, Route, Switch} from "react-router-dom";
import { Home } from "./app";
import { Login, Register } from "./auth";
const R: React.FC = () => {
  return (
    <Router>
      <Switch>
        <Route path="/login">
           <Login />
        </Route>
       <Route path="/register">
           <Register/>
        </Route>

       <Route path="/">
           <Home/>
        </Route>
      </Switch>
    </Router>
  );
};
export default R;

After v6.*

import React from "react";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import { Home } from "./app";
import { Login, Register } from "./auth";
const R: React.FC = () => {
  return (
    <Router>
      <Routes>
        <Route path="/login" caseSensitive={false} element={<Login />} />
        <Route path="/register" caseSensitive={false} element={<Register />} />
        <Route path="/" caseSensitive={false} element={<Home />} />
      </Routes>
    </Router>
  );
};
export default R;

Solution 13 - Reactjs

You have to check npm package version first. To check, run npm info react-router-dom version.

If the package version is >= 6.0.0, you have to change

import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";

to

import { BrowserRouter as Router,Routes, Route, Link } from "react-router-dom";

And also have to change

<Route path="/home" component={Home} />

to

<Route path="/home" element={<Home/>} />

Solution 14 - Reactjs

in react-router-dom v6 Switch is Replaced with Routes

and component with element

{componentName} with {}

Solution 15 - Reactjs

Write <Routes> instead of <Switch>.

Run this in the terminal:

npm install --save react-router react-router-dom

This helped me. Or check file package.json and add the following right after "react-dom": "^17.0.2",:

 "react-router": "^6.0.0",

Solution 16 - Reactjs

Switch is exported from react-router and not react-router-dom (the same goes for Route I think).

Solution 17 - Reactjs

If you are using react-router-dom v6, you have to change Switch to Routes as given in the example below:

Instead of importing Switch, import { Switch, ... } from 'react-router-dom';

import Routes import { Routes, ...} from 'react-router-dom';

Then, instead of using component = {ComponentName}, use element={<ComponentName/>} as shown below:

import { Routes, Route, ...} from 'react-router-dom';
    .
    .
    .  
    <Routes>
      <Route exact path='/' element={<Home/>}></Route>
    </Routes>

Solution 18 - Reactjs

If you are using version 6 of react-router-dom, then you need to update Switch with Routes. The below syntax worked for me:

import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import './App.css';
import Home from './components/Home';

function App() {
  return (
    <div>
      <Router>
        <Routes>
          <Route path="/" exact element={<Home />} />
        </Routes>
      </Router>
    </div>
  );
}

export default App;

Solution 19 - Reactjs

I had the same issue. On the project terminal, type the following commands. You will not need to make any changes to your code.

  1. npm uninstall react-router-dom

  2. npm install [email protected]

Solution 20 - Reactjs

What is your react-router-dom version?

"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",

If it is above one then you need to remove node-modules and reinstall node-module using npm install --save.

Solution 21 - Reactjs

Switch has been replaced by Routes

Source from Update routing (react-router-dom syntax) #1386 (howtographql GitHub)

Enter image description here

Solution 22 - Reactjs

I solved the problem like this:

yarn add react-router-dom@5,3,0

Solution 23 - Reactjs

import {
      BrowserRouter as Router,
      Routes,
      Route,
      Link
    } from "react-router-dom";
    
    function App() {
      return (
        <>
        <Router className="App">
          <Navbar/>
          <Routes>
            <Route path='/'>
    
            </Route>
          </Routes>
        </Router>
        </>
      );
    }
    
    export default App;

Solution 24 - Reactjs

A solution:

Delete the node_modules folder. In the package.json file, change the react-router-dom version (version 6 in my case) to "react-router-dom": "^5.2.1"

Then in the terminal run:

npm install to install the dependencies and then run npm start to relaunch

Solution 25 - Reactjs

This is actually not a problem with you or React or your code. It's just the updated version of react-router-dom. Replace the Switch by Routes.

That’s it. Just use Routes instead of Switch and it works fine.

Solution 26 - Reactjs

I got this error after installing react-router-dom v6.0.2.

Replacing Switch with Routes solved the issue:

import {BrowserRouter as Router, Route, Routes} from "react-router-dom";

Solution 27 - Reactjs

If you are using a newer version of react-router-dom (for example, ^6.2.1 in my case) you should change <Switch> to <Routes> and use <Route> with the component={<SampleComponent />} parameter.

Particularly the code example below:

import { BrowserRouter as Router, Routes, Route} from 'react-router-dom';
import MyComponent from './containers/MyComponent';

export default function AppRoutes() {
    return (
        <Routes>
            <Route exact path="/" component={<MyComponent />}>
            </Route>
        </Routes>
    );
}

Solution 28 - Reactjs

/*Step 1: Step 1. Upgrade to React 16.8+ and react-router-dom v5.2

    npm uninstall react-router-dom
    npm install [email protected]

/*Step 2: Update the react-router-dom import statement.*/
//Change import { Switch, Route } from "react-router-dom";
//to

    import { Routes ,Route } from 'react-router-dom';

/*Step 3: Upgrade the syntax and replace "Switch" with "Routes" and "component" with "element" */
//Change

    // <Switch>
    //          <Route path="/home" component={HomePage} />
    // </Switch>

//to
 

    <Routes>
              <Route path="/home" element={<HomePage/>} />
    </Routes>

//Alternatively you can also downgrade the react-router-version as suggested in other solutions,
//However, rather than going backwards, I would suggest to upgrade to latest version of react-router-dom and its syntax.

Solution 29 - Reactjs

If you installed react-router and react-router-dom v6 beta, just uninstall like this:

npm uninstall --save react-router react-router-dom

And install it with this:

npm install --save react-router react-router-dom

It is going to install the latest version that is not beta.

Then we just need to restart Visual Studio Code or whichever text editor you handle it with.

Solution 30 - Reactjs

I have faced the same issue and I have removed react-router-dom and then reinstalled it.

The issue occurred because I have upgraded it to the latest version, i.e., react-router-dom v6 and then wanted to downgrade to the previous version, i.e., v5.

I was on Ruby on Rails project and using yarn to manage dependency you can use this command to remove and reinstall it:

yarn remove react-router-dom

yarn add react-router-dom

This should solve the issue.

Solution 31 - Reactjs

I was facing the same issue as the issue poster. I tried all things like below:

  • uninstall and install react-router-dom
  • separately imported Switch from react-router-dom

But nothing really worked for me.

Kindly follow the below instructions. I am sure you will not get the error again.

Correct code:

import React from "react";
import "./App.css";
import NavBar from "./components/navbar.js";
import Footer from "./components/footer.js";
import Home from "./components/pages/homepage/home.js";
import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom";

function App() {
  return (
    <Router>
      <div className="app-container">
        <NavBar />
        <Routes>
          <Route path="/home" element={<Home/>} />
        </Routes>
        <Footer />
      </div>
    </Router>
  );
}

export default App;

Note: Switch have been replaced with Routes and instead of using component, we need to use element.

Solution 32 - Reactjs

I was also facing that issue and finally solved it, by arranging code. I am very new in React.

Following is my App.js code (class base component):

import logo from './logo.svg';
import './App.css';

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Navbar from './components/Navbar';
import News from './components/News';
import { BrowserRouter as Router, Route, Routes,Link } from 'react-router-dom'

export class App extends Component {
  static propTypes = {

  }

  render() {
    return (
      <>
        <Navbar/>
        <Router>
          <Routes>
            <Route path="/general"><News pageSize={3} country={'us'} category={'general'}/></Route>
          </Routes>
        </Router>
      </>
    )
  }
}

export default App

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionajesamannView Question on Stackoverflow
Solution 1 - Reactjsprabhat kumar jenaView Answer on Stackoverflow
Solution 2 - Reactjsluke.masView Answer on Stackoverflow
Solution 3 - ReactjsJean LeonView Answer on Stackoverflow
Solution 4 - ReactjsKhizr AA ShaikhView Answer on Stackoverflow
Solution 5 - ReactjsAWS PSView Answer on Stackoverflow
Solution 6 - ReactjsEmmaccenView Answer on Stackoverflow
Solution 7 - ReactjsSuraj AdhikaryView Answer on Stackoverflow
Solution 8 - ReactjsHasnath abdullah akndView Answer on Stackoverflow
Solution 9 - ReactjsMateo VerdaguerView Answer on Stackoverflow
Solution 10 - ReactjsANOOP NAYAKView Answer on Stackoverflow
Solution 11 - ReactjsJude OkaguView Answer on Stackoverflow
Solution 12 - ReactjscrispengariView Answer on Stackoverflow
Solution 13 - ReactjsMd.Shakil ShaikhView Answer on Stackoverflow
Solution 14 - ReactjsUsama TayyabView Answer on Stackoverflow
Solution 15 - ReactjsHM NomaanView Answer on Stackoverflow
Solution 16 - Reactjsknown-as-bmfView Answer on Stackoverflow
Solution 17 - ReactjsAbebe KayimoView Answer on Stackoverflow
Solution 18 - ReactjsDeepView Answer on Stackoverflow
Solution 19 - ReactjsMd. Imrul KayesView Answer on Stackoverflow
Solution 20 - ReactjsNamrata SanjaView Answer on Stackoverflow
Solution 21 - ReactjsAbhishek YadavView Answer on Stackoverflow
Solution 22 - Reactjsmoses salehView Answer on Stackoverflow
Solution 23 - ReactjsRomil JainView Answer on Stackoverflow
Solution 24 - ReactjshanumanDevView Answer on Stackoverflow
Solution 25 - ReactjsAyemun Hossain AshikView Answer on Stackoverflow
Solution 26 - ReactjsyaachView Answer on Stackoverflow
Solution 27 - ReactjsYahor MView Answer on Stackoverflow
Solution 28 - ReactjsAnon17View Answer on Stackoverflow
Solution 29 - ReactjsChrisView Answer on Stackoverflow
Solution 30 - ReactjsAnand SoniView Answer on Stackoverflow
Solution 31 - ReactjsSridhar RajaramView Answer on Stackoverflow
Solution 32 - ReactjsEngr Saddam ZardariView Answer on Stackoverflow