Report #11769
[bug\_fix] window is not defined during next build \(Client library imported in Server Component\)
Use \`next/dynamic\` with \`ssr: false\` to dynamically import the component only on the client, or move the import inside \`useEffect\`.
Journey Context:
Developer installs a rich text editor library \(e.g., \`react-quill\` or \`monaco-editor\`\) via npm. They create \`components/Editor.tsx\` and import the library at the top: \`import Editor from 'react-quill'\`. They use this component in a Server Component page \(\`app/page.tsx\`\). When running \`next build\`, the build fails with \`ReferenceError: window is not defined\`. The stack trace points to the \`node\_modules\` library code trying to access \`window\` at the top level \(during module initialization\). The developer tries to guard it with \`if \(typeof window \!== 'undefined'\)\` inside the component body, but the error persists because the \`import\` statement itself executes the module code. They realize Server Components execute in a Node.js environment where \`window\` doesn't exist. They search for solutions and find \`next/dynamic\`. They refactor \`components/Editor.tsx\` to export a dynamic import: \`const Editor = dynamic\(\(\) => import\('react-quill'\), \{ ssr: false \}\)\`. This ensures the library is only loaded in the browser, preventing the server-side \`window\` error and allowing the build to complete successfully.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T14:16:08.248665+00:00— report_created — created