javascriptreactjsreact-router-domvite

React-router-dom is not working with Vite


I'm building an web application besed on React and Vite. I also use react-router-dom to create a navigation into my app but when I go to the main page I've got this error in my browser's console:

Uncaught SyntaxError: The requested module '/node_modules/.vite/deps/react-router-dom.js?v=67b79ac4' does not provide an export named 'Redirect'

I'm trying to make a navigation into my React app with react-router-dom. Here is my App.jsx code :

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

import Header from "./routes/Header/Header";
import Home from "./routes/Home/Home.jsx";
import AboutMe from "./routes/AboutMe/AboutMe.jsx";
import Portfolio from "./routes/Portfolio/Portfolio.jsx";
import Socials from "./routes/Socials/Socials.jsx";

function App() {

  return (
    <div className="App">
      <Router>
        <Header />
        <Switch>
          <Route exact path="/" component={Home} />
          <Route exace path="/about-me" component={AboutMe} />
          <Route exact path="/portfolio" component={Portfolio} />
          <Route exact path="/socials" component={Socials} />
          <Redirect to="/" />
        </Switch>
      </Router>
    </div>
  )
}

export default App

And here is all my dependencies in the package.json file :

"dependencies": {
  "react": "^18.2.0",
  "react-dom": "^18.2.0",
  "react-router-dom": "^6.5.0",
  "sass": "^1.57.0"
},
"devDependencies": {
  "@types/react": "^18.0.26",
  "@types/react-dom": "^18.0.9",
  "@vitejs/plugin-react": "^3.0.0",
  "vite": "^4.0.0"
}

So can someone explain me why my navigation is not working and how can I resolve the problem ?


Solution

  • Redirect was a feature of version 5 of react-router-dom. from your package.json file i can see you're using version 6. you have to do it like this import { redirect } from "react-router-dom";

    you can learn more from their official documentatuion