Compile web front failed with "ReferenceError: window is not defined" #6461 #6410

Closed
opened 2026-02-21 18:15:36 -05:00 by yindo · 1 comment
Owner

Originally created by @huanshare on GitHub (Oct 29, 2024).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.10.2

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

node version v21.7.3
npm@10.5.0

when I excute this:
npm run build

✔️ Expected Behavior

no error

Actual Behavior

Generating static pages (11/22) [== ]ReferenceError: window is not defined
at /Users/admin/IdeaProjects/dify/web/.next/server/chunks/2399.js:1:17416
at 73425 (/Users/admin/IdeaProjects/dify/web/.next/server/chunks/2399.js:1:17824)
at t (/Users/admin/IdeaProjects/dify/web/.next/server/webpack-runtime.js:1:143)
at 6132 (/Users/admin/IdeaProjects/dify/web/.next/server/chunks/7061.js:113:10333)
at t (/Users/admin/IdeaProjects/dify/web/.next/server/webpack-runtime.js:1:143)
at 27086 (/Users/admin/IdeaProjects/dify/web/.next/server/app/(commonLayout)/tools/page.js:1:11343)
at Object.t [as require] (/Users/admin/IdeaProjects/dify/web/.next/server/webpack-runtime.js:1:143)
at require (/Users/admin/IdeaProjects/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:16:18669)
at i (/Users/admin/IdeaProjects/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:88294)
at /Users/admin/IdeaProjects/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:98817 {
digest: '383294202'
}

Originally created by @huanshare on GitHub (Oct 29, 2024). ### Self Checks - [X] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.10.2 ### Cloud or Self Hosted Self Hosted (Source) ### Steps to reproduce node version v21.7.3 npm@10.5.0 when I excute this: npm run build ### ✔️ Expected Behavior no error ### ❌ Actual Behavior Generating static pages (11/22) [== ]ReferenceError: window is not defined at /Users/admin/IdeaProjects/dify/web/.next/server/chunks/2399.js:1:17416 at 73425 (/Users/admin/IdeaProjects/dify/web/.next/server/chunks/2399.js:1:17824) at t (/Users/admin/IdeaProjects/dify/web/.next/server/webpack-runtime.js:1:143) at 6132 (/Users/admin/IdeaProjects/dify/web/.next/server/chunks/7061.js:113:10333) at t (/Users/admin/IdeaProjects/dify/web/.next/server/webpack-runtime.js:1:143) at 27086 (/Users/admin/IdeaProjects/dify/web/.next/server/app/(commonLayout)/tools/page.js:1:11343) at Object.t [as require] (/Users/admin/IdeaProjects/dify/web/.next/server/webpack-runtime.js:1:143) at require (/Users/admin/IdeaProjects/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:16:18669) at i (/Users/admin/IdeaProjects/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:88294) at /Users/admin/IdeaProjects/dify/web/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:98817 { digest: '383294202' }
yindo added the 🤔 cant-reproduce label 2026-02-21 18:15:36 -05:00
yindo closed this issue 2026-02-21 18:15:36 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Oct 29, 2024):

Hello, @huanshare! I'm here to assist you with any bugs, questions, or contribution guidance you may need. Let's tackle this issue together!

To resolve the "ReferenceError: window is not defined" error when running "npm run build" for a self-hosted Dify version 0.10.2, you need to ensure that any code accessing window or document is only executed in a browser environment. This error occurs because window and document are not available in a Node.js environment, which is used during the build process.

You can fix this by checking if globalThis.document is defined before accessing it. Here's an example of how you can modify the code to prevent the error:

if (typeof globalThis.document !== 'undefined' && globalThis.document.body?.getAttribute('data-api-prefix') && globalThis.document.body?.getAttribute('data-pubic-api-prefix')) {
  apiPrefix = globalThis.document.body.getAttribute('data-api-prefix') as string;
  publicApiPrefix = globalThis.document.body.getAttribute('data-pubic-api-prefix') as string;
}

This ensures that the code accessing document is only executed when document is available, preventing the "ReferenceError: window is not defined" during the build process [1].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Oct 29, 2024): <!-- Greeting --> Hello, @huanshare! I'm here to assist you with any bugs, questions, or contribution guidance you may need. Let's tackle this issue together! <!-- Answer --> To resolve the "ReferenceError: window is not defined" error when running "npm run build" for a self-hosted Dify version 0.10.2, you need to ensure that any code accessing `window` or `document` is only executed in a browser environment. This error occurs because `window` and `document` are not available in a Node.js environment, which is used during the build process. You can fix this by checking if `globalThis.document` is defined before accessing it. Here's an example of how you can modify the code to prevent the error: ```ts if (typeof globalThis.document !== 'undefined' && globalThis.document.body?.getAttribute('data-api-prefix') && globalThis.document.body?.getAttribute('data-pubic-api-prefix')) { apiPrefix = globalThis.document.body.getAttribute('data-api-prefix') as string; publicApiPrefix = globalThis.document.body.getAttribute('data-pubic-api-prefix') as string; } ``` This ensures that the code accessing `document` is only executed when `document` is available, preventing the "ReferenceError: window is not defined" during the build process <sup>[[1]](https://github.com/langgenius/dify/blob/main/web/config/index.ts)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#6410