In this post, we will see How To Fix - "Attempted Import Error: 'Switch' is Not Exported From 'react-router-dom' in React.js, React-Router-Dom. Various facets of the same error.
Attempted import error: 'Switch' is not exported from 'react-router-dom'.
export 'Switch' (imported as 'Switch') was not found in 'react-router-dom'
Attempted import error: 'Routes' is not exported from 'react-router-dom'.
import error: 'Switch' is not exported from 'react-router'.
There could be possible reasons for the issue which we will explore and try to fix.
npm info react-router-dom version
window.React.version
import React from 'react';
console.log(React.version);
For React < V6
import { Switch } from "react-router-dom";
For React >= V6
import { Routes } from 'react-router-dom';
For React < V6
<Route exact path="/" component={Home} />
Change to
For React >= V6
import { BrowserRouter as Router, Routes, Route} from 'react-router-dom';
<Route path="/" element={<Home />} />
For React < V6
<Switch>
<Route path="/home" component={WriteToUs} />
</Switch>
Change to
For React >= V6
<Routes>
<Route path="/home" element={<WriteToUs/>} />
</Routes>
<Navigate replace to="/" />
In case, you want to replicate react-router-dom version 5 "Redirect" component redirecting from a path, then you would have to combine with a "Route". See below -
<Route path="/<path\_name>" element={<Navigate replace to="/" />} />
import { BrowserRouter as Router } from "react-router-dom";
import { Switch, Route, Routes } from "react-router-dom";
npm uninstall react-router-dom
npm install [email protected]
e.g.
npm install [email protected]
Hope this helps to solve the error.
attempted import error 'switch' is not exported from 'react-router-dom' (imported as 'switch') ,attempted import error 'button' is not exported from 'react-router-dom' , ,attempted import error: 'usehistory' is not exported from 'react-router-dom'. ,react router-dom v6 ,attempted import error: 'usenavigate' is not exported from 'react-router-dom'. ,react-router-dom switch ,react-router-dom not working ,attempted import error: 'uselocation' is not exported from 'react-router-dom ,how to uninstall react-router-dom ,react router-dom routes ,attempted import error 'switch' is not exported from 'react-router-dom' ,attempted import error 'usehistory' is not exported from 'react-router-dom' ,attempted import error 'redirect' is not exported from 'react-router-dom' ,attempted import error 'withrouter' is not exported from 'react-router-dom' ,attempted import error 'usenavigate' is not exported from 'react-router-dom' ,attempted import error 'useroutematch' is not exported from 'react-router-dom' ,attempted import error 'uselocation' is not exported from 'react-router-dom' ,attempted import error 'useparams' is not exported from 'react-router-dom' ,attempted import error 'browserhistory' is not exported from 'react-router-dom' ,attempted import error 'switch' is not exported from 'react-router-dom' ,attempted import error 'usehistory' is not exported from 'react-router-dom' ,attempted import error 'redirect' is not exported from 'react-router-dom' ,attempted import error 'withrouter' is not exported from 'react-router-dom' ,attempted import error 'usenavigate' is not exported from 'react-router-dom' ,attempted import error 'useroutematch' is not exported from 'react-router-dom' ,attempted import error 'uselocation' is not exported from 'react-router-dom' ,attempted import error 'useparams' is not exported from 'react-router-dom' ,attempted import error 'browserhistory' is not exported from 'react-router-dom' , , ,Attempted Import Error: 'Switch' is Not Exported From 'react-router-dom' ,attempted import error 'switch' is not exported from 'react-router-dom' ,attempted import error 'button' is not exported from 'react-router-dom' ,attempted import error 'switch' is not exported from 'react-router-dom' (imported as 'switch') ,attempted import error 'switch' is not exported from 'react-router-dom'. error ,attempted import error 'switch' is not exported from 'react-router-dom'. in react js ,attempted import error 'switch' is not exported from 'react-router-dom'. react ,attempted import error 'switch' is not exported from 'react-router-dom'. v6 ,Attempted Import Error: Not Exported From 'react-router-dom' , ,Attempted Import Error: Not Exported From 'react-router-dom' ,attempted import error is not exported from 'react-router-dom' ,attempted import error 'switch' is not exported from 'react-router-dom' ,attempted import error 'usehistory' is not exported from 'react-router-dom' ,attempted import error 'redirect' is not exported from 'react-router-dom' ,attempted import error 'route' is not exported from 'react-router-dom' ,attempted import error 'withrouter' is not exported from 'react-router-dom' ,attempted import error 'usenavigate' is not exported from 'react-router-dom' ,attempted import error 'uselocation' is not exported from 'react-router-dom' ,attempted import error 'switch' is not exported from 'react-router-dom' (imported as 'switch') ,attempted import error 'redirect' is not exported from 'react-router-dom' (imported as 'redirect') ,attempted import error 'routes' is not exported from 'react-router-dom' (imported as 'routes') ,attempted import error 'navigate' is not exported from 'react-router-dom' (imported as 'navigate') ,attempted import error 'browser' is not exported from 'react-router-dom' (imported as 'router') ,attempted import error 'browserhistory' is not exported from 'react-router-dom' ,attempted import error 'browserrouter' is not exported from 'react-router-dom' ,attempted import error 'button' is not exported from 'react-router-dom' ,attempted import error 'container' is not exported from 'react-router-dom' ,attempted import error 'dropdown' is not exported from 'react-router-dom' ,attempted import error 'switch' is not exported from 'react-router-dom'. error ,attempted import error 'hashhistory' is not exported from 'react-router-dom' ,attempted import error 'history' is not exported from 'react-router-dom' ,attempted import error 'switch' is not exported from 'react-router-dom'. in react js ,attempted import error 'link' is not exported from 'react-router-dom' ,attempted import error 'linkcontainer' is not exported from 'react-router-dom' ,attempted import error 'use location' is not exported from 'react-router-dom' ,attempted import error 'match' is not exported from 'react-router-dom' ,attempted import error 'outlet' is not exported from 'react-router-dom' ,okta attempted import error 'useroutematch' is not exported from 'react-router-dom' ,attempted import error 'prompt' is not exported from 'react-router-dom' ,attempted import error 'privateroute' is not exported from 'react-router-dom' ,attempted import error '\_\_routercontext' is not exported from 'react-router-dom' ,attempted import error 'routers' is not exported from 'react-router-dom' ,attempted import error 'routerlink' is not exported from 'react-router-dom' ,attempted import error 'switch' is not exported from 'react-router-dom'. react ,attempted import error 'usehistory' is not exported from 'react-router-dom'. react ,react attempted import error 'withrouter' is not exported from 'react-router-dom' ,attempted import error 'useroutematch' is not exported from 'react-router-dom' ,attempted import error 'useparams' is not exported from 'react-router-dom' ,attempted import error 'useroutes' is not exported from 'react-router-dom' ,attempted import error 'usesearchparams' is not exported from 'react-router-dom' ,attempted import error 'use prompt' is not exported from 'react-router-dom' ,attempted import error 'switch' is not exported from 'react-router-dom'. v6 ,attempted import error 'indexroute' is not exported from 'react-router-dom' ,attempted import error 'index link' is not exported from 'react-router-dom' , ,attempted import error '\_\_routercontext' is not exported from 'react-router-dom' ,attempted import error 'browser' is not exported from 'react-router-dom' (imported as 'router') ,attempted import error 'browserhistory' is not exported from 'react-router-dom' ,attempted import error 'browserrouter' is not exported from 'react-router-dom' ,attempted import error 'button' is not exported from 'react-router-dom' ,attempted import error 'container' is not exported from 'react-router-dom' ,attempted import error 'dropdown' is not exported from 'react-router-dom' ,attempted import error 'hashhistory' is not exported from 'react-router-dom' ,attempted import error 'history' is not exported from 'react-router-dom' ,attempted import error 'link' is not exported from 'react-router-dom' ,attempted import error 'linkcontainer' is not exported from 'react-router-dom' ,attempted import error 'match' is not exported from 'react-router-dom' ,attempted import error 'navigate' is not exported from 'react-router-dom' ,attempted import error 'navigate' is not exported from 'react-router-dom' (imported as 'navigate') ,attempted import error 'outlet' is not exported from 'react-router-dom' ,attempted import error 'privateroute' is not exported from 'react-router-dom' ,attempted import error 'prompt' is not exported from 'react-router-dom' ,attempted import error 'redirect' is not exported from 'react-router-dom' ,attempted import error 'redirect' is not exported from 'react-router-dom' (imported as 'redirect') ,attempted import error 'route' is not exported from 'react-router-dom' ,attempted import error 'routerlink' is not exported from 'react-router-dom' ,attempted import error 'routers' is not exported from 'react-router-dom' ,attempted import error 'routes' is not exported from 'react-router-dom' (imported as 'routes') ,attempted import error 'switch' is not exported from 'react-router-dom' ,attempted import error 'switch' is not exported from 'react-router-dom' (imported as 'switch') ,attempted import error 'switch' is not exported from 'react-router-dom'. error ,attempted import error 'switch' is not exported from 'react-router-dom'. in react js ,attempted import error 'switch' is not exported from 'react-router-dom'. react ,attempted import error 'switch' is not exported from 'react-router-dom'. v6 ,attempted import error 'use location' is not exported from 'react-router-dom' ,attempted import error 'use prompt' is not exported from 'react-router-dom' ,attempted import error 'useblocker' is not exported from 'react-router-dom' ,attempted import error 'usecontext' is not exported from 'react-router-dom' ,attempted import error 'usehistory' is not exported from 'react-router-dom' ,attempted import error 'usehistory' is not exported from 'react-router-dom'. react ,attempted import error 'uselocation' is not exported from 'react-router-dom' ,attempted import error 'usenavigate' is not exported from 'react-router-dom' ,attempted import error 'useparams' is not exported from 'react-router-dom' ,attempted import error 'useroutematch' is not exported from 'react-router-dom' ,attempted import error 'useroutes' is not exported from 'react-router-dom' ,attempted import error 'usesearchparams' is not exported from 'react-router-dom' ,attempted import error 'useselector' is not exported from 'react-router-dom' ,attempted import error 'withrouter' is not exported from 'react-router-dom' ,attempted import error is not exported from 'react-router-dom' ,okta attempted import error 'useroutematch' is not exported from 'react-router-dom' ,react attempted import error 'withrouter' is not exported from 'react-router-dom' , , ,attempted import error 'button' is not exported from 'react-router-dom' ,attempted import error 'switch' is not exported from 'react-router-dom' ,attempted import error 'switch' is not exported from 'react-router-dom' (imported as 'switch') ,attempted import error 'switch' is not exported from 'react-router-dom'. error ,attempted import error 'switch' is not exported from 'react-router-dom'. in react js ,attempted import error 'switch' is not exported from 'react-router-dom'. react ,attempted import error 'switch' is not exported from 'react-router-dom'. v6 ,react attempted import error 'switch' is not exported from 'react-router-dom' ,react error attempted import error 'switch' is not exported from 'react-router-dom' ,react js attempted import error 'switch' is not exported from 'react-router-dom'