Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

router.push() does nothing or throws in a Next.js app

ErrorError: NextRouter was not mounted

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

  • Applies to Next.js 15, Next.js 16, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 import
  2. 2Match what you see, then jump to its fix

    1. 1Error: NextRouter was not mounteduseRouter is imported from next/routerFix
    2. 2router.push sits in a Server Component, or in a Server Action or route handler.The navigation is attempted on the serverFix
    3. 3The URL changes, but the page content is the old one.The new page shows stale data after the pushFix

What does the error look like?

  • Error: NextRouter was not mounted
  • router.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 dev

Confirmed by nextjs.org

Did this fix it?

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=*.tsx

2Fix

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 5

Confirmed by nextjs.org

Did this fix it?

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 dev

Confirmed by nextjs.org

Did this fix it?

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:

Related fixes

Web 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 fix

Web 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.