How to Fix the TypeError: "Module 'react-router-dom' has no exported member 'Switch'."

How to Fix the TypeError: "Module 'react-router-dom' has no exported member 'Switch'."

While working on a project that required the creation of routes with React router, I ran into this error;

TypeError: Module 'react-router-dom' has no exported member 'Switch'.

During the debugging process, it turned out the solution was quite a simple one. The Switch component in react-router-dom v5 was replaced with Routes in v6. So to fix this error, you'll need to update the import from;

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

to

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

Also in v6, the Route component declaration was replaced with element. So, you'll need to update it as shown below;

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

Once all of these are in place, the error should be gone!.

To learn more, visit the official docs here