Error: NextRouter was not mounted (experimental-edge runtime)
Answered
Spectacled bear posted this in #help-forum
Spectacled bearOP
I'm trying to get NextJS to SRR correctly.
I have tried changing the router to next/navigation, then I get "[Error: invariant expected app router to be mounted]" (I'm still using the pages directory)
it works as normal when removing the "export const runtime = "experimental-edge"", but unfortunately that isn't an option.
NextJS Version: v13.4.10 (via next --version)
Full error log(s)
I have tried changing the router to next/navigation, then I get "[Error: invariant expected app router to be mounted]" (I'm still using the pages directory)
it works as normal when removing the "export const runtime = "experimental-edge"", but unfortunately that isn't an option.
NextJS Version: v13.4.10 (via next --version)
Full error log(s)
next/router
:- ready started server on 0.0.0.0:80, url: http://localhost:80
- info Loaded env from D:\Projects\Web\redacted\site\.env.local
- event compiled client and server successfully in 624 ms (18 modules)
- wait compiling...
- event compiled client and server successfully in 416 ms (18 modules)
- wait compiling /404 (client and server)...
- warn You are using an experimental edge runtime, the API might change.
- wait compiling / (client and server)...
- event compiled client and server successfully in 18.3s (3044 modules)
[Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted]
[Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted]
next/navigation
- ready started server on 0.0.0.0:80, url: http://localhost:80
- info Loaded env from D:\Projects\Web\redacted\site\.env.local
- event compiled client and server successfully in 516 ms (18 modules)
- wait compiling...
- event compiled client and server successfully in 250 ms (18 modules)
- warn You are using an experimental edge runtime, the API might change.
- wait compiling / (client and server)...
- event compiled client and server successfully in 7.6s (2934 modules)
[Error: invariant expected app router to be mounted]
[Error: invariant expected app router to be mounted]
Answered by joulev
maybe the syntax is wrong –Â
export const runtime
only works in the app router perhaps; in the pages router you need export const config
like in my screenshot50 Replies
Spectacled bearOP
I had it like that initially, but it told me to change it to 'experimental-edge' and failed to compile
ill change it and send a log if you'd like
OHH you are using the pages router, then you have to use
next/router
hmmmmmbut
next/router
doesn't work hmmSpectacled bearOP
> Build error occurred
Error: Page /explore/[[...explore]] provided runtime 'edge', the edge runtime for rendering is currently experimental. Use runtime 'experimental-edge' instead.
at getPageStaticInfo (D:\Projects\Web\redacted\site\node_modules\next\dist\build\analysis\get-page-static-info.js:401:23)
at async getStaticInfoIncludingLayouts (D:\Projects\Web\redacted\site\node_modules\next\dist\build\entries.js:81:28)
at async D:\Projects\Web\redacted\site\node_modules\next\dist\build\entries.js:362:32
at async Promise.all (index 13)
at async Promise.all (index 1)
at async createEntrypoints (D:\Projects\Web\redacted\site\node_modules\next\dist\build\entries.js:482:5)
at async Span.traceAsyncFn (D:\Projects\Web\redacted\site\node_modules\next\dist\trace\trace.js:103:20)
at async webpackBuildImpl (D:\Projects\Web\redacted\site\node_modules\next\dist\build\webpack-build\impl.js:102:25)
at async webpackBuild (D:\Projects\Web\redacted\site\node_modules\next\dist\build\webpack-build\index.js:137:16)
at async D:\Projects\Web\redacted\site\node_modules\next\dist\build\index.js:586:123
- info Creating an optimized production build .
Spectacled bearOP
and thats using the pages directory?
yeah
maybe the syntax is wrong –Â
export const runtime
only works in the app router perhaps; in the pages router you need export const config
like in my screenshotAnswer
Spectacled bearOP
only difference I can see is you did
export const config { runtime: "experimental-edge"}
instead of export const runtime = 'experimental-edge'
weird that they apparently removed the documentation on this
Spectacled bearOP
I'll give it a shot
but if my memory serves me well,
export const config
is the way to declare runtime in the pages routerSpectacled bearOP
hm, alright. I honestly couldn't say, I initially had it in the
next.config.js
as experimental.runtime, but then I upgraded to the latest version and the only thing I could find mentioned export const runtime = ...
weird that it says
edge
here though, i can confirm i had to use experimental-edge
Spectacled bearOP
Yup, I got past that initial error. Now my problem is with sentry
but I think that problem is for another day. thank you for your help!

TypeError: eg.wrapGetServerSidePropsWithSentry is not a function
but I think that problem is for another day. thank you for your help!
Spectacled bearOP
Oh wait I forgot the initial error was "NextRouter was not mounted.", not compiling issues.
it still doesn't work, lmao
it still doesn't work, lmao
@joulev this works for me
uhmm but this works for me in both dev mode and prod mode
Spectacled bearOP
Odd, I just built it and ran it and the only that displays in-browser is "Internal server error", and console gets spammed with
[Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted]
[Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted]
[Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted]
[Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted]
can you give me a minimal reproduction repository?
because as you see – my code works fine for me
Spectacled bearOP
Sure, one sec
Spectacled bearOP
Got it successfully reproduced, uploading it to git
Thanks for this, ill have a look as soon as i can
Spectacled bearOP
Yup, no problem. I should be the one thanking you, so thank you
interestingly it works normally for me
do you use windows?
i tested on macos (my code screenshot) and linux (your repo), both work fine
Spectacled bearOP
Yes, windows 10
So you can run it and the webpage shows up normally?
yeah
then it looks like it is a bug on windows only; please report it
Spectacled bearOP
Oh yeah, the second image
I’ll try a few things and then make a bug report tomorrow, thank you for your help
if you deploy on vercel, try it – vercel runs on linux and should work
Spectacled bearOP
What version of node are you on?
Spectacled bearOP
alright, i'll try with those versions. ty
Got it working with node v20.5.0
I was on node v18.16.0 before, so I suppose it doesn't work with that version
Spectacled bearOP
but only in WSL, lmao