web build error [ ReferenceError: window is not defined ] #18

Closed
opened 2026-02-16 11:19:31 -05:00 by yindo · 2 comments
Owner

Originally created by @ssssmy on GitHub (Aug 15, 2024).

➜ web git:(main) ✗ npm run build
yarn run v1.22.22
$ next build
▲ Next.js 14.2.4

  • Environments: .env.local

Creating an optimized production build ...
✓ Compiled successfully
Skipping validation of types
Skipping linting
✓ Collecting page data
Generating static pages (0/15) [ ]ReferenceError: window is not defined
at /Users/shenmingyuan/docker-data/dify/web/.next/server/chunks/90.js:1:22372
at 53950 (/Users/shenmingyuan/docker-data/dify/web/.next/server/chunks/90.js:1:22780)
at t (/Users/shenmingyuan/docker-data/dify/web/.next/server/webpack-runtime.js:1:143)
at 55378 (/Users/shenmingyuan/docker-data/dify/web/.next/server/chunks/6364.js:122:10331)
at t (/Users/shenmingyuan/docker-data/dify/web/.next/server/webpack-runtime.js:1:143)
at 8894 (/Users/shenmingyuan/docker-data/dify/web/.next/server/app/(commonLayout)/tools/page.js:1:11769)
at Object.t [as require] (/Users/shenmingyuan/docker-data/dify/web/.next/server/webpack-runtime.js:1:143)
at require (/Users/shenmingyuan/docker-data/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:16:18365)
at I (/Users/shenmingyuan/docker-data/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:94362)
at /Users/shenmingyuan/docker-data/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:96668 {
digest: '89040208'
}
Generating static pages (10/15) [= ]
Error occurred prerendering page "/tools". Read more: https://nextjs.org/docs/messages/prerender-error

ReferenceError: window is not defined
at /Users/shenmingyuan/docker-data/dify/web/.next/server/chunks/90.js:1:22372
at 53950 (/Users/shenmingyuan/docker-data/dify/web/.next/server/chunks/90.js:1:22780)
at t (/Users/shenmingyuan/docker-data/dify/web/.next/server/webpack-runtime.js:1:143)
at 55378 (/Users/shenmingyuan/docker-data/dify/web/.next/server/chunks/6364.js:122:10331)
at t (/Users/shenmingyuan/docker-data/dify/web/.next/server/webpack-runtime.js:1:143)
at 8894 (/Users/shenmingyuan/docker-data/dify/web/.next/server/app/(commonLayout)/tools/page.js:1:11769)
at Object.t [as require] (/Users/shenmingyuan/docker-data/dify/web/.next/server/webpack-runtime.js:1:143)
at require (/Users/shenmingyuan/docker-data/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:16:18365)
at I (/Users/shenmingyuan/docker-data/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:94362)
at /Users/shenmingyuan/docker-data/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:96668
✓ Generating static pages (15/15)

Export encountered errors on following paths:
/(commonLayout)/tools/page: /tools
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Originally created by @ssssmy on GitHub (Aug 15, 2024). ➜ web git:(main) ✗ npm run build yarn run v1.22.22 $ next build ▲ Next.js 14.2.4 - Environments: .env.local Creating an optimized production build ... ✓ Compiled successfully Skipping validation of types Skipping linting ✓ Collecting page data Generating static pages (0/15) [ ]ReferenceError: window is not defined at /Users/shenmingyuan/docker-data/dify/web/.next/server/chunks/90.js:1:22372 at 53950 (/Users/shenmingyuan/docker-data/dify/web/.next/server/chunks/90.js:1:22780) at t (/Users/shenmingyuan/docker-data/dify/web/.next/server/webpack-runtime.js:1:143) at 55378 (/Users/shenmingyuan/docker-data/dify/web/.next/server/chunks/6364.js:122:10331) at t (/Users/shenmingyuan/docker-data/dify/web/.next/server/webpack-runtime.js:1:143) at 8894 (/Users/shenmingyuan/docker-data/dify/web/.next/server/app/(commonLayout)/tools/page.js:1:11769) at Object.t [as require] (/Users/shenmingyuan/docker-data/dify/web/.next/server/webpack-runtime.js:1:143) at require (/Users/shenmingyuan/docker-data/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:16:18365) at I (/Users/shenmingyuan/docker-data/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:94362) at /Users/shenmingyuan/docker-data/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:96668 { digest: '89040208' } Generating static pages (10/15) [= ] Error occurred prerendering page "/tools". Read more: https://nextjs.org/docs/messages/prerender-error ReferenceError: window is not defined at /Users/shenmingyuan/docker-data/dify/web/.next/server/chunks/90.js:1:22372 at 53950 (/Users/shenmingyuan/docker-data/dify/web/.next/server/chunks/90.js:1:22780) at t (/Users/shenmingyuan/docker-data/dify/web/.next/server/webpack-runtime.js:1:143) at 55378 (/Users/shenmingyuan/docker-data/dify/web/.next/server/chunks/6364.js:122:10331) at t (/Users/shenmingyuan/docker-data/dify/web/.next/server/webpack-runtime.js:1:143) at 8894 (/Users/shenmingyuan/docker-data/dify/web/.next/server/app/(commonLayout)/tools/page.js:1:11769) at Object.t [as require] (/Users/shenmingyuan/docker-data/dify/web/.next/server/webpack-runtime.js:1:143) at require (/Users/shenmingyuan/docker-data/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:16:18365) at I (/Users/shenmingyuan/docker-data/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:94362) at /Users/shenmingyuan/docker-data/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:96668 ✓ Generating static pages (15/15) > Export encountered errors on following paths: /(commonLayout)/tools/page: /tools error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
yindo added the bug label 2026-02-16 11:19:31 -05:00
yindo closed this issue 2026-02-16 11:19:31 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 15, 2024):

Hey @ssssmy! I'm Dosu, a bot here to help you with bug fixes, questions, and becoming a contributor. Let's squash those bugs together! 🤖

The error ReferenceError: window is not defined indicates that there is an attempt to access the window object in a server-side context, which is not available. This is a common issue when using browser-specific APIs in server-rendered pages.

To resolve this, you should:

  1. Check for Direct References to window: Ensure that there are no direct references to the window object within the /tools page or its components .

  2. Use Client-Side Only Logic: If you need to use the window object, make sure to wrap the code in a check to ensure it only runs on the client side. For example:

    if (typeof window !== 'undefined') {
        // Your code that uses window
    }
    
  3. Custom Hooks: Verify if any custom hooks used in the /tools page are accessing the window object and ensure they are also wrapped in client-side checks .

  4. Next.js Configurations: Review any custom Next.js configurations or plugins that might affect the server-side rendering process for the /tools page .

  5. Environment Variables: Check if there are any specific environment variables or configurations in the .env.local file that might affect the rendering behavior of the /tools page .

By addressing these points, you should be able to resolve the window is not defined error during the build process.

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Aug 15, 2024): <!-- Greeting --> Hey @ssssmy! I'm [Dosu](https://dosu.dev), a bot here to help you with bug fixes, questions, and becoming a contributor. Let's squash those bugs together! 🤖 <!-- Answer --> The error `ReferenceError: window is not defined` indicates that there is an attempt to access the `window` object in a server-side context, which is not available. This is a common issue when using browser-specific APIs in server-rendered pages. To resolve this, you should: 1. **Check for Direct References to `window`:** Ensure that there are no direct references to the `window` object within the `/tools` page or its components . 2. **Use Client-Side Only Logic:** If you need to use the `window` object, make sure to wrap the code in a check to ensure it only runs on the client side. For example: ```javascript if (typeof window !== 'undefined') { // Your code that uses window } ``` 3. **Custom Hooks:** Verify if any custom hooks used in the `/tools` page are accessing the `window` object and ensure they are also wrapped in client-side checks . 4. **Next.js Configurations:** Review any custom Next.js configurations or plugins that might affect the server-side rendering process for the `/tools` page . 5. **Environment Variables:** Check if there are any specific environment variables or configurations in the `.env.local` file that might affect the rendering behavior of the `/tools` page . By addressing these points, you should be able to resolve the `window is not defined` error during the build process. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@zhansc commented on GitHub (Aug 26, 2024):

我这边有出现这个问题,通过更新node版本,和dify/web/package.json中指定的版本保持一致,解决了这个问题
原来的node版本:
web % node -v
v22.5.1

重新安装后的版本:
web % node -v
v18.20.4

@zhansc commented on GitHub (Aug 26, 2024): 我这边有出现这个问题,通过更新node版本,和dify/web/package.json中指定的版本保持一致,解决了这个问题 原来的node版本: web % node -v v22.5.1 重新安装后的版本: web % node -v v18.20.4
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-docs-archived#18