I get this error when I try to build (run build) my nextjs application. But I have a lot of files/pages and this error message doesn’t say much to me. How do I find the exact location where this error is occurring. And what is the cause of this error?
Generating static pages (38/47) [== ]
ReferenceError: document is not defined
at e.exports (/Users/cardoso/workspace/qf/packages/application/.next/server/chunks/3871.js:1:68626)
at 95136 (/Users/cardoso/workspace/qf/packages/application/.next/server/chunks/3871.js:1:154901)
at t (/Users/cardoso/workspace/qf/packages/application/.next/server/webpack-runtime.js:1:143)
at 83151 (/Users/cardoso/workspace/qf/packages/application/.next/server/chunks/5895.js:9:283)
at t (/Users/cardoso/workspace/qf/packages/application/.next/server/webpack-runtime.js:1:143)
at F (/Users/cardoso/workspace/qf/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:91937)
at $ (/Users/cardoso/workspace/qf/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:90488)
at rC (/Users/cardoso/workspace/qf/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:33840)
at n$ (/Users/cardoso/workspace/qf/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:59861)
at nj (/Users/cardoso/workspace/qf/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:64628)
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
ReferenceError: document is not defined
at e.exports (/Users/cardoso/workspace/qf/packages/application/.next/server/chunks/3871.js:1:68626)
at 95136 (/Users/cardoso/workspace/qf/packages/application/.next/server/chunks/3871.js:1:154901)
at t (/Users/cardoso/workspace/qf/packages/application/.next/server/webpack-runtime.js:1:143)
at 83151 (/Users/cardoso/workspace/qf/packages/application/.next/server/chunks/5895.js:9:283)
at t (/Users/cardoso/workspace/qf/packages/application/.next/server/webpack-runtime.js:1:143)
at F (/Users/cardoso/workspace/qf/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:91937)
at $ (/Users/cardoso/workspace/qf/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:90488)
at rC (/Users/cardoso/workspace/qf/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:33840)
at n$ (/Users/cardoso/workspace/qf/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:59861)
at nj (/Users/cardoso/workspace/qf/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:64628)
There are more errors like this, the message is very long. But they are basically the same as these
2
Answers
if you are on server component, you have no access to
window
object. (document is property on window, window.document. if you are on client component, since the client component is first rendered on the server, you have to set a guard before reaching
document`same issue here
Generating static pages (8/12) [== ]
ReferenceError: window is not defined
at 56192 (D:Hitekthankyoujpnextjs-sourcebase-cms.nextserverchunks343.js:1:8616)
at webpack_require (D:Hitekthankyoujpnextjs-sourcebase-cms.nextserverwebpack-runtime.js:1:161)
at 6728 (D:Hitekthankyoujpnextjs-sourcebase-cms.nextserverapp(dashboard)dashboardsettingspage.js:1:3199)
at webpack_require (D:Hitekthankyoujpnextjs-sourcebase-cms.nextserverwebpack-runtime.js:1:161)
at A (D:Hitekthankyoujpnextjs-sourcebase-cmsnode_modulesnextdistcompilednext-serverapp-page.runtime.prod.js:35:5655)
at D:Hitekthankyoujpnextjs-sourcebase-cmsnode_modulesnextdistcompilednext-serverapp-page.runtime.prod.js:35:8070
at H._fromJSON (D:Hitekthankyoujpnextjs-sourcebase-cmsnode_modulesnextdistcompilednext-serverapp-page.runtime.prod.js:35:8466)
at JSON.parse ()
at I (D:Hitekthankyoujpnextjs-sourcebase-cmsnode_modulesnextdistcompilednext-serverapp-page.runtime.prod.js:35:5406)
at t (D:Hitekthankyoujpnextjs-sourcebase-cmsnode_modulesnextdistcompilednext-serverapp-page.runtime.prod.js:35:11719)
Error occurred prerendering page "/dashboard/settings". Read more: https://nextjs.org/docs/messages/prerender-error
ReferenceError: window is not defined
at 56192 (D:Hitekthankyoujpnextjs-sourcebase-cms.nextserverchunks343.js:1:8616)
at webpack_require (D:Hitekthankyoujpnextjs-sourcebase-cms.nextserverwebpack-runtime.js:1:161)
at 6728 (D:Hitekthankyoujpnextjs-sourcebase-cms.nextserverapp(dashboard)dashboardsettingspage.js:1:3199)
at webpack_require (D:Hitekthankyoujpnextjs-sourcebase-cms.nextserverwebpack-runtime.js:1:161)
at A (D:Hitekthankyoujpnextjs-sourcebase-cmsnode_modulesnextdistcompilednext-serverapp-page.runtime.prod.js:35:5655)
at D:Hitekthankyoujpnextjs-sourcebase-cmsnode_modulesnextdistcompilednext-serverapp-page.runtime.prod.js:35:8070
at H._fromJSON (D:Hitekthankyoujpnextjs-sourcebase-cmsnode_modulesnextdistcompilednext-serverapp-page.runtime.prod.js:35:8466)
at JSON.parse ()
at I (D:Hitekthankyoujpnextjs-sourcebase-cmsnode_modulesnextdistcompilednext-serverapp-page.runtime.prod.js:35:5406)
at t (D:Hitekthankyoujpnextjs-sourcebase-cmsnode_modulesnextdistcompilednext-serverapp-page.runtime.prod.js