Maaz Zindani

Loading the index…

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

"invariant expected app router to be mounted" in a Next.js app

ErrorUncaught Error: invariant expected app router to be mounted

Short answer

This error appears when a component calls useRouter imported from next/router while it renders inside the App Router. The App Router has its own router, exported from next/navigation. Switch the import, or use next/compat/router for a component shared between the pages and app directories.

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

Fix it fast

  1. 1Find out which cause you have

    Find every file that imports the Pages Router hook, then check whether it renders under the app directory.

    grep -rn "from 'next/router'" app components src 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1The error appears on a page in the app directory, and the component imports useRouter from 'next/router'.useRouter is imported from next/router in a component under app/Fix
    2. 2After switching the import, query and pathname are undefined, or router.events does not exist.The code reads router.query, router.pathname or router.eventsFix
    3. 3The same component works under pages/ and throws under app/, and you cannot switch the import yet.One component is shared between pages/ and app/Fix

What does the error look like?

  • Uncaught Error: invariant expected app router to be mounted

Cause 1 of 3

useRouter is imported from next/router in a component under app/

1Check

The docs state that useRouter must be imported from next/navigation, not next/router, when using the App Router.

grep -rn "next/router" app

2Fix

Change the import and mark the file as a Client Component, because hooks only work there.

'use client'

import { useRouter } from 'next/navigation'

export default function Page() {
  const router = useRouter()
  return (
    <button type="button" onClick={() => router.push('/dashboard')}>
      Dashboard
    </button>
  )
}

3Verify

Reload the page; the click navigates and the console is quiet.

npm run dev

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 3

The code reads router.query, router.pathname or router.events

1Check

The router from next/navigation does not have the pathname string, the query object or router.events. Each has a separate hook or replacement.

grep -rn "router\.query\|router\.pathname\|router\.events\|router\.asPath" app components

2Fix

Use usePathname for the path and useSearchParams for query values. For events, combine those hooks in an effect.

'use client'
import { usePathname, useSearchParams } from 'next/navigation'

export function Where() {
  const pathname = usePathname()
  const searchParams = useSearchParams()
  return <p>{pathname}?{searchParams.toString()}</p>
}

3Verify

The path and query values show on the page.

npm run dev

Confirmed by nextjs.org

Did this fix it?

Cause 3 of 3

One component is shared between pages/ and app/

1Check

next/router throws when the Pages Router is not mounted. next/compat/router is the same hook, usable in both directories, and returns NextRouter or null instead of throwing.

grep -rln "useRouter" components | head

2Fix

Import from next/compat/router, treat the result as possibly null, and read search values with useSearchParams from next/navigation.

import { useEffect } from 'react'
import { useRouter } from 'next/compat/router'
import { useSearchParams } from 'next/navigation'

export function Search() {
  const router = useRouter() // NextRouter or null
  const searchParams = useSearchParams()
  useEffect(() => {
    if (router && !router.isReady) return
    const search = searchParams.get('search')
    // use search
  }, [router, searchParams])
  return null
}

3Verify

Render the component once from a pages route and once from an app route; neither throws.

npm run build

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • In new code under app/, import navigation hooks only from next/navigation.
  • Search the project for next/router after moving a page from pages/ to app/.
  • Do not destructure the result of useRouter from next/compat/router; it can be null.

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

Next step

Want this handled for good?

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