Next.js「ReferenceError: window is not defined」の原因と直し方
window is not defined は、ブラウザ専用コードを useEffect 内へ移すか、ssr: false の next/dynamic で読めば直る。
SSR やビルド時のプリレンダで window を参照すると出る。
公開: 更新:
npx next build > build.log 2>&1 grep -E "window is not defined|prerendering page|Export encountered" build.log Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error ReferenceError: window is not defined Export encountered an error on /page: /, exiting the build.
npx next build > build.log 2>&1 grep -E "Compiled successfully|✓ Generating static pages|window is not defined" build.log ✓ Compiled successfully in 2.1s ✓ Generating static pages (4/4)
2026-11-12 に node:20(Next.js 15.5.29 / Node.js 20.20.2) で実際に打って取った出力。検証環境の詳細
要約
ReferenceError: window is not defined は、サーバー側で実行されるコードがブラウザ専用のオブジェクトに触れたときに出る。
Next.js はページをサーバーやビルド時にいったん描画(プリレンダ)する。
その時点に window は存在しない。
直し方は「ブラウザでだけ動かす」ことの徹底である。
実行例
App Router の最小プロジェクトを作り、'use client' を付けたコンポーネントのレンダー本体で window.innerWidth を読んで next build にかけてみる。'use client' があってもビルド時のプリレンダはサーバーで走るため ReferenceError: window is not defined で終了コード 1 になり、useEffect へ移すと同じビルドが通る。
$ npx next build > build.log 2>&1
$ echo $?
1$ grep -E "window is not defined|prerendering page|Export encountered" build.log
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
ReferenceError: window is not defined
Export encountered an error on /page: /, exiting the build.$ npx next build > build.log 2>&1
$ echo $?
0$ grep -E "Compiled successfully|✓ Generating static pages|window is not defined" build.log
✓ Compiled successfully in 2.1s
✓ Generating static pages (4/4)$ npx next build > build.log 2>&1
$ echo $?
1$ grep -E "window is not defined|prerendering page|Export encountered" build.log
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
ReferenceError: window is not defined
Export encountered an error on /page: /, exiting the build.$ npx next build > build.log 2>&1
$ echo $?
0$ grep -E "Compiled successfully|✓ Generating static pages|window is not defined" build.log
✓ Compiled successfully in 2.4s
✓ Generating static pages (4/4)— 2026-11-12 時点の出力
検証環境Node.js 20.20.2 / npm 10.8.2 / Python 3.11.2 / 2026-11-12 検証(ほか 2 件)
- 検証日
- 実行環境
node:20Debian GNU/Linux 12 (bookworm)- バージョン
- Node.js 20.20.2
- npm 10.8.2
- Python 3.11.2
- Git 2.39.5
- Next.js 15.5.29
この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。
解決策
1. useEffect の中へ移す
useEffect はクライアントでのみ実行されるため、ブラウザ API はここに置く。
'use client';
import { useEffect, useState } from 'react';
export function Width() {
const [w, setW] = useState(0);
useEffect(() => {
setW(window.innerWidth); // クライアントでのみ実行
}, []);
return <span>{w}</span>;
}2. ssr:false で動的 import する
コンポーネントごとプリレンダから外したいときは、Client Component から next/dynamic を使う。
'use client';
import dynamic from 'next/dynamic';
const Chart = dynamic(() => import('./Chart'), { ssr: false });3. typeof でガードする
単発の参照なら、アクセス前に存在チェックを入れる。
if (typeof window !== 'undefined') {
// ここはブラウザでのみ動く
}よくある原因
- Server Component やモジュールのトップレベルで
window/document/localStorageを読んでいる。 - ブラウザ前提のライブラリを通常 import し、ビルド時に評価されている。
useEffectの外、つまりレンダー本体でブラウザ API を呼んでいる。