router.push() does nothing or throws in a Next.js app
Error: NextRouter was not mountedShort answer
In the App Router, import useRouter from next/navigation, call it in a Client Component, and use it in event handlers. Importing it from next/router gives a NextRouter was not mounted error. On the server use redirect() instead, and call router.refresh() when the target shows stale data.
Fix it fast
1Find out which cause you have
See which module useRouter comes from in the failing file. In the app directory it must be next/navigation.
grep -rn "useRouter" app/ components/ | grep import2Match what you see, then jump to its fix
- 1
Error: NextRouter was not mounteduseRouter is imported from next/routerFix - 2
router.push sits in a Server Component, or in a Server Action or route handler.The navigation is attempted on the serverFix - 3
The URL changes, but the page content is the old one.The new page shows stale data after the pushFix
- 1
What does the error look like?
Error: NextRouter was not mountedrouter.push() runs but the URL or page does not change
Cause 1 of 3
useRouter is imported from next/router
1Check
Next.js documents that useRouter from next/router inside the app directory behaves differently and raises this error.
grep -rn "from 'next/router'" app/2Fix
Change the import to next/navigation. Its router has no pathname or query: use usePathname and useSearchParams instead.
'use client'
import { useRouter } from 'next/navigation'
export default function Go() {
const router = useRouter()
return <button onClick={() => router.push('/dashboard')}>Dashboard</button>
}3Verify
Click the button and watch the URL change.
npm run devConfirmed by nextjs.org
Cause 2 of 3
The navigation is attempted on the server
1Check
useRouter changes routes inside Client Components. It is not a server API.
grep -rn "router.push" app/ --include=*.tsx2Fix
On the server, call redirect(). It throws, so keep it outside a try/catch block. In a Client Component event handler, keep using useRouter.
import { redirect } from 'next/navigation'
export default async function Page() {
const user = await getUser()
if (!user) redirect('/login')
// ...
}3Verify
Open the page signed out and confirm it lands on /login.
curl -sI http://localhost:3000/ | head -n 5Confirmed by nextjs.org
Cause 3 of 3
The new page shows stale data after the push
1Check
router.refresh() makes a new server request and re-renders Server Components. Look for a mutation that ran just before the push.
grep -rn "router.push" app/ -B3 | grep -i "fetch\|post\|mutation"2Fix
Refresh the route after the mutation, and invalidate server cached data with revalidatePath or revalidateTag if it is cached.
router.push('/posts')
router.refresh()3Verify
Repeat the action and check the list shows the new item.
npm run devConfirmed by nextjs.org
How do I stop it coming back?
- Prefer the Link component for plain navigation and keep useRouter for code-driven moves.
- Never pass untrusted URLs to router.push: javascript: URLs run in your page.
- Use next/navigation in every file under app and next/router only under pages.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
ESLint says an element in a list is missing its key prop
react/jsx-key
react/jsx-key fires when an element returned from map, Array.from or an array literal has no key.
3 causes, each with a fixWeb apps
There is no tailwind.config.js after installing Tailwind 4 and HeroUI in Next.js
HeroUI components render unstyled
Nothing is broken: Tailwind CSS 4 is configured in CSS and no longer creates tailwind.config.js.
3 causes, each with a fixWeb apps
react-i18next shows the wrong plural form for a count, or prints the key instead of the text
t('item') prints the key item instead of a translated sentence
i18next picks a plural from the count option and looks for keys ending in _one, _other and so on.
4 causes, each with a fixWeb apps
useSession throws because there is no SessionProvider above it
[next-auth]: `useSession` must be wrapped in a <SessionProvider />
useSession reads the session from React Context, and SessionProvider is what creates that context.
3 causes, each with a fixWeb apps
next/link throws React.Children.only on its children
Error: React.Children.only expected to receive a single React element child.
React.Children.only accepts exactly one React element and throws for anything else, including an array with one item.
3 causes, each with a fixWeb apps
Hydration failed after adding next-themes dark mode
Console reports a server and client mismatch on the html element
next-themes changes the html element and knows the theme only in the browser, so server HTML and client HTML differ.
3 causes, each with a fix