Troubleshooting

React Router Not Working? Common Errors and Fixes

React Router not working: console error messages next to their fixes

When 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.

Install and upgrade errors

"Cannot find module 'react-router-dom'" or "Failed to resolve import"

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.

npm ERR! ERESOLVE unable to resolve dependency tree

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.

npm WARN EBADENGINE Unsupported engine

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.

"SyntaxError: Cannot use import statement outside a module" in tests

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.

Runtime errors in the browser

"useNavigate() may be used only in the context of a <Router> component."

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.

"You cannot render a <Router> inside another <Router>."

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.

Warning: "No routes matched location "/something""

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.

Nested route renders a blank area

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.

useParams() returns undefined

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.

NavLink is always active on the home link

Why it happens: every URL starts with /, so the home link matches everything.

Fix: add the end prop: <NavLink to="/" end>Home</NavLink>.

Deployment problems

404 Not Found when refreshing a page in production

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.

Assets fail to load on nested URLs

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">).

A quick debugging checklist

  1. Run npm ls react-router react react-dom and confirm versions match the requirements.
  2. Search the project for react-router-dom; in v8 there should be no matches.
  3. Confirm there is exactly one router at the top of the tree.
  4. Check every layout component for <Outlet />.
  5. Add a path="*" route and see whether the URL lands there.
  6. For production-only 404s, check the server's fallback rule.

Download React Router 8.5.3

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.

Download

Tivari

Professional content writer

Tivari is a professional content writer who covers web development tools, with a focus on turning technical documentation into clear, step-by-step articles. Every command and code sample in Tivari's articles is run against the current React Router release before publishing. About the writer and how we test.

Related articles