"invariant expected app router to be mounted" in a Next.js app
Uncaught Error: invariant expected app router to be mountedShort 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.
Fix it fast
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/null2Match what you see, then jump to its fix
- 1
The 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
After 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
The 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
- 1
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" app2Fix
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 devConfirmed by nextjs.org
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 components2Fix
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 devConfirmed by nextjs.org
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 | head2Fix
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 buildConfirmed by nextjs.org
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
Web apps
AuthUserPoolException: Auth UserPool not configured in a Next.js app
Auth UserPool not configured
The error means an Amplify Auth call ran before Amplify.configure did.
3 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb 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 fix