How to Download and Install React Router (Step by Step)
Every way to get React Router running: one npm command, a new Framework Mode app, or an offline install from the downloaded package, with checks at each step.
Read MoreWhen React Router is not working, the cause is usually one of about ten problems, and the error message tells you which one if you know how to read it. This page lists the messages developers hit most often with React Router 8, what each one really means, and the fix. Use your browser's find (Ctrl+F or Cmd+F) to jump to the message you are seeing.
Why it happens: React Router 8 removed the react-router-dom package. Tutorials written for v6 and v7 still import from it, and upgrading react-router to v8 leaves those imports pointing at nothing.
Fix: change the imports.
// Before (v6 / v7)
import { BrowserRouter, Link, RouterProvider } from "react-router-dom";
// After (v8)
import { BrowserRouter, Link } from "react-router";
import { RouterProvider } from "react-router/dom";
Then remove the old package with npm uninstall react-router-dom. A project-wide find and replace of "react-router-dom" with "react-router" handles most files; only RouterProvider and HydratedRouter need react-router/dom.
Why it happens: React Router 8 requires React and React DOM 19.2.7 or newer. npm refuses to install it next to an older React.
Fix: upgrade React (npm i react@latest react-dom@latest), or stay on v7 for now with npm i react-router@7. Do not use --force or --legacy-peer-deps as a fix: the install succeeds, but the app then breaks in confusing ways at runtime.
Why it happens: your Node.js version is older than 22.22.0.
Fix: install a current Node.js release, then delete node_modules and the lockfile's stale entries by running a clean install (rm -rf node_modules && npm i). On CI and hosting platforms, update the Node.js version setting too. See React Router system requirements for the full list.
Why it happens: v8 is ESM-only, and an older Jest setup tries to run it as CommonJS.
Fix: move tests to Vitest, which handles ESM out of the box, or enable Jest's ESM support and make sure react-router is not excluded by transformIgnorePatterns.
The same message appears for useHref(), useLocation() and other hooks, and for <Link>.
Why it happens: the component that uses the hook is rendered outside the router. Common culprits are a header placed next to <BrowserRouter> instead of inside it, and unit tests that render a component on its own.
Fix: make the router the outermost wrapper, usually in main.jsx:
createRoot(root).render(
<BrowserRouter>
<Header /> {/* inside, not beside */}
<App />
</BrowserRouter>
);
In tests, wrap the component in <MemoryRouter> or use createMemoryRouter.
Why it happens: there are two routers, often one in main.jsx and another in App.jsx, or a library component that brings its own.
Fix: keep exactly one BrowserRouter (or one RouterProvider) at the top of the app and delete the other.
Why it happens: the URL does not match any route. Typical causes are a typo, a leading slash inside nested routes, or a missing catch-all route.
Fix: check the spelling, use relative paths for children (path="billing", not path="/billing" inside a settings parent unless you write the full path), and add <Route path="*" element={<NotFound />} /> so unknown URLs get a friendly page instead of a warning.
Why it happens: the URL matches the child route, but the parent component never renders <Outlet />, so the child has nowhere to appear. In development React Router logs "Matched leaf route at location ... does not have an element or Component" when the route itself has nothing to render.
Fix: add <Outlet /> to the parent layout where the child content belongs, and make sure every leaf route has an element or Component.
Why it happens: the param name in the component does not match the route path (:userId versus id), or the component is rendered by a route that does not include that segment.
Fix: make the names match exactly, and read the param in a component that is rendered by, or nested under, the route that declares it.
Why it happens: every URL starts with /, so the home link matches everything.
Fix: add the end prop: <NavLink to="/" end>Home</NavLink>.
Why it happens: in a single-page app, only index.html exists on the server. A refresh on /dashboard asks the server for a file called dashboard, which does not exist.
Fix: tell the server to serve index.html for unknown paths. On Apache, add this to .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]
On Nginx, use try_files $uri /index.html;. Most static hosts offer a "single-page app" or rewrite setting that does the same thing. Framework Mode with server rendering avoids the problem entirely, because the server knows every route.
Why it happens: the app is deployed in a subfolder, or assets use relative paths, so /people/ada looks for scripts in /people/.
Fix: set Vite's base option to the deployment path and pass the same value to basename on your router (<BrowserRouter basename="/app">).
npm ls react-router react react-dom and confirm versions match the requirements.react-router-dom; in v8 there should be no matches.<Outlet />.path="*" route and see whether the URL lands there.Still stuck? Re-run the clean setup in How to Download and Install React Router in a new folder. If the fresh project works, compare it with yours to find the difference. For a refresher on how the pieces fit together, the beginner's walkthrough builds a working app from scratch.
Get React Router 8.5.3: one npm command, or the verified package download.
DownloadRelated articles
Every way to get React Router running: one npm command, a new Framework Mode app, or an offline install from the downloaded package, with checks at each step.
Read MoreBuild a small multi-page React app from scratch and learn Routes, Link, NavLink, Outlet, useParams and useNavigate along the way.
Read MoreA plain-English tour of what React Router can do, from nested layouts and URL params to loaders, actions, type-safe routes and server rendering.
Read More