Next.js Discord

Discord Forum

`next build` fails after formatting with `prettier` due to `ReferenceError: window is not defined`

Answered
Camuise posted this in #help-forum
Open in Discord
My project was building perfectly fine so I quickly ran a npx prettier . --write to format my code before pushing. However, after formatting, my project no longer builds.

The issue is that next build fails to generate static pages for all of my routes with almost the exact same error, the only difference being the page route at the end (substituted with <PAGE_ROUTE>):
ReferenceError: window is not defined
    at G (C:\Users\Marcus\Repos\website\.next\server\chunks\114.js:1:18080)
    at nj (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:46251)
    at nM (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:47571)
    at nN (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:64546)
    at nB (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:67538)
    at nD (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:66680)
    at nN (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:64853)
    at nB (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:67538)
    at nM (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:58560)
    at nN (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:64546)
ReferenceError: window is not defined
    at G (C:\Users\Marcus\Repos\website\.next\server\chunks\114.js:1:18080)
    at nj (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:46251)
    at nM (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:47571)
    at nN (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:64546)
    at nB (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:67538)
    at nD (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:66680)
    at nN (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:64853)
    at nB (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:67538)
    at nM (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:58560)
    at nN (C:\Users\Marcus\Repos\website\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:12:64546) {
  digest: '3756917605'
}

Error occurred prerendering page "<PAGE_ROUTE>". Read more: https://nextjs.org/docs/messages/prerender-error


Here's my .prettierrc in case it helps:
{
  "tabWidth": 2,
  "useTabs": false,
  "plugins": ["prettier-plugin-tailwindcss"]
}


What's going on and how do I fix it?
Answered by Camuise
Ok I think I figured it out. turns out that somehow I forgot about a useState(window.innerWidth < 768); I had called in my custom navbar component and for some reason the dev server either didn't yell at me about or I didn't notice. whoops.
View full answer

3 Replies

Boost because I still haven't figured it out
Gharial
@Camuise Are you using the window object anywhere in your code? Might wanna search your repo to be safe. If not, it might be tedious but you might wanna just turn prettier on on save and go through and save each file to try and isolate the issue
Ok I think I figured it out. turns out that somehow I forgot about a useState(window.innerWidth < 768); I had called in my custom navbar component and for some reason the dev server either didn't yell at me about or I didn't notice. whoops.
Answer