最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Next.js – window is not defined error even when using useEffect and typeof window !== 'undefined' c

programmeradmin0浏览0评论

I am working on a Next.js project where I am encountering the error: Error occurred prerendering page "/dashboard". Read more: ReferenceError: window is not defined at new u (D:\Projects\FREELANCE\ROAD RUNNER RSA\Frontend\.next\server\chunks\500.js:13:9122)

  1. What I Have Tried Ensured the code runs only in the browser by wrapping window inside useEffect:
useEffect(() => {
  if (typeof window !== "undefined") {
    const scrollbarWidth =
      window.innerWidth - document.documentElement.clientWidth;
    setScrollbarWidth(scrollbarWidth);
  }
}, []);
  1. Checked that the component is a client component by adding "use client" at the top of the file:
"use client";
import { useEffect, useState } from "react";

3.Checked third-party libraries (lucide-react, framer-motion, next/navigation, and @radix-ui/react-visually-hidden) to ensure none of them are accessing window on the server.

I am working on a Next.js project where I am encountering the error: Error occurred prerendering page "/dashboard". Read more: https://nextjs./docs/messages/prerender-error ReferenceError: window is not defined at new u (D:\Projects\FREELANCE\ROAD RUNNER RSA\Frontend\.next\server\chunks\500.js:13:9122)

  1. What I Have Tried Ensured the code runs only in the browser by wrapping window inside useEffect:
useEffect(() => {
  if (typeof window !== "undefined") {
    const scrollbarWidth =
      window.innerWidth - document.documentElement.clientWidth;
    setScrollbarWidth(scrollbarWidth);
  }
}, []);
  1. Checked that the component is a client component by adding "use client" at the top of the file:
"use client";
import { useEffect, useState } from "react";

3.Checked third-party libraries (lucide-react, framer-motion, next/navigation, and @radix-ui/react-visually-hidden) to ensure none of them are accessing window on the server.

Share Improve this question asked Feb 2 at 17:00 Rohan PrajapatiRohan Prajapati 11 silver badge2 bronze badges 2
  • 3 Did you try out the solution proposed by the link in the error? nextjs./docs/messages/… – 3limin4t0r Commented Feb 2 at 17:03
  • This question is similar to: Next.js 13 "window is not defined". If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – derpirscher Commented Feb 2 at 22:10
Add a comment  | 

2 Answers 2

Reset to default 1
  • Avoid Module-Level Window References: Ensure that no code accesses window outside of useEffect or other browser-only functions.
  • Review Imported Components: Check if any imported component (even in client components) references window at the module level.
  • Use Dynamic Imports: For components that require window,

import dynamic from 'next/dynamic';
const ClientOnlyComponent = dynamic(() => import('./YourComponent'), { ssr: false });

  • Check _app.js and _document.js: Ensure these files don’t have any server-side window references.

Answer: The issue was lenis library, when I created its object it was not in the useEffect hook! Now I have used useEffect and it's working well!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论