Next.js: NextRouter was not mounted
Next.js says NextRouter was not mounted? useRouter from next/router ran outside Next.js, in a test, or in the app directory. Use next/navigation there.
Reviewed October 24, 2026
What it means in plain English
Next.js says this error occurs when a component used useRouter outside a Next.js application, or was rendered outside a Next.js application.
It can happen in unit tests of components that use useRouter, because the tests are not configured with Next.js's contexts. It can also happen when you use useRouter from next/router inside the app directory. The App Router's useRouter from next/navigation behaves differently from the useRouter hook in pages.
Likely causes
- A unit test renders a component that calls
useRouter. - A file in the
appdirectory importsuseRouterfromnext/router. - A component in
pagesis being moved toapp, and the import still points atnext/router.
How to fix it, step by step
- Find which
useRouterthe component imports. - In the
appdirectory, migrate to the hooks imported fromnext/navigation.
import { useRouter } from 'next/navigation'
- In a test, mock the
useRouter()hook fromnext/router. Next.js linksnext-router-mockfor this. - In the
pagesdirectory, while you migrate frompagestoapp, you can import fromnext/compat/routerinstead. Next.js says it is the sameuseRouterhook but does not throw this error.
How to check it worked
- The page or test renders without
NextRouter was not mounted. - Components in
appimport router hooks fromnext/navigation.
Sources
Related fixes
More Next.js errors fixes
Keep going on your own.
The guides stay free. A deep leak report is $15. Fix packs are $24, or $49 for all three. There is no done-for-you repair.