Next.js Discord

Discord Forum

Getting "NextRouter was not mounted" error after moving existing Next.js app into an existing Turbo

Unanswered
Pembroke Welsh Corgi posted this in #help-forum
Open in Discord
Pembroke Welsh CorgiOP
Late last year, our team created a new mono repo with Turbo Repo to house our front end applications and npm packages. Up until now, everything in that repo has been green field/net new applications and packages.

I'm trying to move one of our other front end applications into the mono repo, a Next.js project, but when I try to run it locally I'm getting the following error:

Error: NextRouter was not mounted.

This error doesn't occur when running the project in it's own repository. I even cloned the project in a new directory and did a fresh npm install with both the original versions used in the project and the latest versions of Next and React. There is one other Next.js project in the Turbo Repo that is the latest version of Next, while the project I'm trying to move was on v13.

The older project also has both pages and app router sections of the app (we plan to move the older pages code into the app router), but it shouldn't trigger that error. I've verified the `useRouter` hooks are being imported from the proper library (`next/router` or `next/navigation` depending on if it's pages or app router).

Here's the full stack trace:

    Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted
        at useRouter (/Users/me/Projects/company/mono_repo/node_modules/next/dist/client/router.js:146:15)
        at useHybridHydrate (/Users/me/Projects/company/mono_repo/node_modules/next-redux-wrapper/lib/index.js:250:45)
        at useWrappedStore (/Users/me/Projects/company/mono_repo/node_modules/next-redux-wrapper/lib/index.js:298:9)
        at WrappedComponent (/Users/me/Projects/company/mono_repo/node_modules/next-redux-wrapper/lib/index.js:321:22)
        at /Users/me/Projects/company/mono_repo/node_modules/@sentry/nextjs/cjs/common/wrapPageComponentWithSentry.js:72:27
        at /Users/me/Projects/company/mono_repo/node_modules/@sentry/node/cjs/async/hooks.js:42:14
        at AsyncLocalStorage.run (node:async_hooks:346:14)
        at Object.runWithAsyncContext (/Users/me/Projects/company/mono_repo/node_modules/@sentry/node/cjs/async/hooks.js:41:25)
        at Object.runWithAsyncContext (/Users/me/Projects/company/mono_repo/node_modules/@sentry/core/cjs/hub.js:763:36)
        at Object.apply (/Users/me/Projects/company/mono_repo/node_modules/@sentry/nextjs/cjs/common/wrapPageComponentWithSentry.js:58:21)
        at renderWithHooks (/Users/me/Projects/company/mono_repo/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
        at renderIndeterminateComponent (/Users/me/Projects/company/mono_repo/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
        at renderElement (/Users/me/Projects/company/mono_repo/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
        at renderNodeDestructiveImpl (/Users/me/Projects/company/mono_repo/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
        at renderNodeDestructive (/Users/me/Projects/company/mono_repo/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14) {
      page: '/'

1 Reply

Pembroke Welsh CorgiOP
The app does load if I remove the /pages directory. Not sure where the disconnect is in the mono-repo vs stand alone.

I also still get this error even if I remove any instance of useRouter throughout the app (pages and app router).