To add React Router to a Vite project, install the react-router package and mount a BrowserRouter. The setup has three parts: install react-router, mount a BrowserRouter, and declare routes. This guide walks through the whole path on a fresh Vite app.
Create the Vite app
Run the create command, pick a name, and choose the React template with the JavaScript variant.
Install react-router
Add the package from npm inside the new project folder.
Mount BrowserRouter
Wrap the App component in main.jsx so the router controls navigation.
Declare routes
Add Routes and Route in App.jsx and link between the pages.
Scaffold and install
Scaffold the app first. Vite asks for a project name, a framework, and a language variant, so choose React and JavaScript when prompted. Vite requires Node.js 20.19 or newer in the 20.x line, or 22.12 or newer in the 22.x line.
npm create vite@latest my-router-app -- --template react
cd my-router-app
npm install react-routerThe first command creates the project with the React template, which skips the prompts. The second command installs react-router, the single package that now contains every React Router export.
Mount BrowserRouter
Open main.jsx and wrap App in a BrowserRouter. This one change makes the router available to the whole tree, so any component can read the URL or trigger navigation.
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { BrowserRouter } from "react-router";
import App from "./App.jsx";
createRoot(document.getElementById("root")).render(
<StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</StrictMode>
);BrowserRouter keeps the URL bar in sync with what your components render. Mount it only once in main.jsx, not inside App or any page component.
Declare routes
Replace the default App.jsx content with two small pages and a Routes block. The page components are plain React functions, and each Route maps a path to one of them.
function Home() {
return <h1>Home</h1>;
}
function About() {
return <h1>About</h1>;
}With the pages defined, render them through Routes. The first Route matches the root path, and the second matches /about.
import { Routes, Route } from "react-router";
export default function App() {
return (
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
);
}Routes and Route are the core of a React Router app. A fuller explanation of paths, links, and layouts is in the React Router tutorial.
Add links
Users need a way to move between pages. Add a Link for each route so clicks update the URL without a full page reload.
import { Link } from "react-router";
export default function Nav() {
return (
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
</nav>
);
}Render Nav above Routes in App.jsx, or keep it in a shared layout. Clicking Home or About now swaps the visible page and updates the address bar.
Run the dev server
Start Vite and open the printed local URL, usually http://localhost:5173.
npm run devVisit /about directly or click the About link. The page should change without a network request for a new document. Opening a path with no route renders nothing, which is the cue to add a catch-all route, covered in how to build a 404 page with React Router.
Troubleshooting
- A "Cannot find module react-router" error means the package was not installed. Run npm install react-router from the project folder.
- A blank page on every path usually means the app is missing BrowserRouter. Check main.jsx.
- Imports from react-router-dom are outdated. Import from react-router instead.
- A path that renders nothing has no matching Route. Add the missing route or a catch-all.
Rune AI
Key Insights
- Scaffold a Vite React app, then install react-router.
- Wrap the root component in BrowserRouter inside main.jsx.
- Declare paths with Routes and Route in App.jsx.
- Use Link for navigation and add a catch-all route for unknown paths.
Frequently Asked Questions
Which package do I install?
Does BrowserRouter need to wrap the whole app?
What happens when I open a URL with no matching route?
Conclusion
Adding React Router to a Vite project is three steps: install react-router, wrap the app in BrowserRouter, and declare routes with Routes and Route. Run the dev server and check that links update the URL without reloading the page.
More in this topic
How to Build a Dropdown Menu in React
Build a React dropdown menu with the ARIA menu button pattern. Handle open and close, keyboard arrows, and clicks outside the menu.
How to Animate React Components with Motion
Animate React components with the Motion library. Set up motion, add enter, hover, and exit animations, and respect reduced motion.
Headless UI Components Explained: Logic Without Locked Styling
Understand headless UI components and how libraries like Radix give you unstyled, accessible behavior that you style yourself.