hi, i trying to devloy my next js 14 app on vercel but i get this error during deploy on build :
code
ReferenceError: window is not defined
at /vercel/path0/.next/server/app/profile/address/page.js:11:15553
at 65025 (/vercel/path0/.next/server/app/profile/address/page.js:11:162010)
at t (/vercel/path0/.next/server/webpack-runtime.js:1:143)
at 69069 (/vercel/path0/.next/server/app/profile/address/page.js:6:549)
at t (/vercel/path0/.next/server/webpack-runtime.js:1:143)
at F (/vercel/path0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:94693)
at /vercel/path0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:97108
at W._fromJSON (/vercel/path0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:97546)
at JSON.parse (<anonymous>)
at N (/vercel/path0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:94414)
Error occurred prerendering page "/profile/address". Read more: https://nextjs.org/docs/messages/prerender-error
ReferenceError: window is not defined
at /vercel/path0/.next/server/app/profile/address/page.js:11:15553
at 65025 (/vercel/path0/.next/server/app/profile/address/page.js:11:162010)
at t (/vercel/path0/.next/server/webpack-runtime.js:1:143)
at 69069 (/vercel/path0/.next/server/app/profile/address/page.js:6:549)
at t (/vercel/path0/.next/server/webpack-runtime.js:1:143)
at F (/vercel/path0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:94693)
at /vercel/path0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:97108
at W._fromJSON (/vercel/path0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:97546)
at JSON.parse (<anonymous>)
at N (/vercel/path0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:94414)
(node:1262) NOTE: The AWS SDK for JavaScript (v2) will enter maintenance mode
on September 8, 2024 and reach end-of-support on September 8, 2025.
Please migrate your code to use AWS SDK for JavaScript (v3).
For more information, check blog post at https://a.co/cUPnyil
✓ Generating static pages (67/67)
> Export encountered errors on following paths:
/profile/address/page: /profile/address
Error: Command "npm run build" exited with 1
Assistant
The error message you're encountering indicates that there is code in your Next.js application that is trying to access the object on the server side. In Next.js, code can be run on both the client and server side, and the object is only available on the client side. This often happens when prerendering static pages, as the build process tries to execute the pages in a server-side context where is not defined.
User
Assistant
User
the only think i guess it because of it this imports :
code
import {
LayersControl,
MapContainer,
Marker,
Popup,
TileLayer,
useMap,
useMapEvents,
} from "react-leaflet";
import "leaflet/dist/leaflet.css";
import L from "leaflet";
how i import them dynamicly on client without ssr
Assistant
window
window
window
Here's how you can address the issue:
Check for Client-Side Code:
Ensure that any code accessing the window object (and potentially other browser-specific objects like document) is only executed on the client side. You can use useEffect or conditional statements to check if the code is running in a browser before accessing window.
javascript
import { useEffect } from 'react';
const MyComponent = () => {
useEffect(() => {
// This code runs only on the client side
console.log(window.location.href);
}, []);
return (
<div>
Your component code here.
</div>
);
};
export default MyComponent;
Dynamic imports:
If you have a component or module that depends on the window object, you can dynamically import it so that it only runs on the client side.
Check your dependencies:
Sometimes third-party libraries might access window during their initialization. Ensure that you're using isomorphic libraries or importing such libraries dynamically as shown above.
Verify server and client-side separation:
If you're conditionally rendering components or using data fetching, make sure that you account for these differences explicitly. Separate server-side logic from client-side logic cleanly.
Review pages and components:
Go through the files, especially those mentioned in the error stack (e.g., /profile/address/page.js), to identify where window might be accessed, and apply the above solutions.
Following these steps should help you resolve the "window is not defined" error during the build process on Vercel. Once resolved, try deploying again and the build should proceed successfully.
im not use window on profile/address/page.js this is code of all the component im use in page.js :
page.js:
code
"use client";
import { Plus } from 'lucide-react';
import UserProfileCard from "@/components/card/userProfileCard";
import React from "react";
import AddressList from "./addressList";
import { Button } from "@nextui-org/react";
import { profileEditMoadalAction } from "@/store/profileEditModal-slice";
import { useDispatch } from "react-redux";
import AddAddressForm from "./addAddressForm";
const UserProfileAddress = () => {
const dispatch = useDispatch();
const openModal = () => {
dispatch(profileEditMoadalAction.openModal("addressForm"));
};
return (
<UserProfileCard title="آدرس ها">
<div className="w-full h-full flex flex-col justify-center items-center">
<div className='self-end ml-20 m-4'>
<Button onClick={openModal} color="primary">
اضافه کردن آدرس
<Plus />
</Button>
<AddAddressForm/>
</div>
<AddressList />
</div>
</UserProfileCard>
);
};
export default UserProfileAddress;
that all the component i use in profile/address/page.js and page.js code itself
The error you're encountering suggests that there might be some code that implicitly relies on browser-specific APIs like window, but from the code snippets you've provided, there doesn't seem to be any direct reference to window. However, the error might be originating from a library or hook that is implicitly trying to use the window object, especially since you are using libraries that could have such dependencies.
Here are some potential causes and solutions:
Check Third-Party Libraries:
Libraries like react-leaflet could be using window under the hood. While your component uses use client directives (making React components on the client-side), make sure that they are always executed in a client-side environment. Verify that any configuration or initialization of such libraries doesn't assume access to the window object.
Dynamic Imports:
If any component or module uses window in its initialization phase, make sure to use dynamic imports with client-side rendering only, especially for components using potentially problematic libraries.
Check for Unintended SSR Code Execution:
Even though you're using "use client", ensure nothing is trying to execute SSR-specific code inadvertently. Inspect modules and their dependencies for client-side only capabilities.
Check Node Environment:
Sometimes checking the environment before interacting with specific APIs can help mitigate the error:
js
if (typeof window !== 'undefined') {
// Safe to use window
}
Inspect Library Versions/Updates:
Some libraries may have patches that fix SSR issues. Check if updates are available or if there are any known issues with server-side rendering.
Verbose Logging:
Add console logs or breakpoints within the components that might log additional information on where the dev server might encounter the access to window.
javascript
useEffect(() => {
console.log("Component is rendering on the client");
}, []);
Global Variables or State:
If there's external code (not shown here) or configuration affecting your modules, it might be worth reviewing these settings.
Ensure each component, especially imported ones, properly checks the client context before any window operations. If deploying on Vercel and logging doesn't yield insights, try running next build locally to capture and inspect similar errors in detail.
To address potential issues with libraries like react-leaflet that may depend on browser-specific features such as window or document, you can dynamically import them in a way that ensures they are only used on the client side, preventing server-side rendering (SSR) issues. Here’s how you can do it using Next.js’s dynamic imports:
Create a dynamic import for your map component: Instead of importing react-leaflet components directly in your module, dynamically import the whole map component.
Use dynamic with ssr: false: This tells Next.js to only load the component on the client side, avoiding SSR.
Here's a step-by-step guide to dynamically import react-leaflet:
1. Create the Map Component
Assume you have a separate component file MapComponent.js or similar for your map logic. Modify this component accordingly to use dynamic imports.
No Direct Server Imports: Ensure that none of react-leaflet or leaflet modules are imported directly in files that are server-rendered.
Dynamic Component Use: Use the dynamic import provided by Next.js to control where and when these libraries are loaded and rendered.
Map Events: Ensure all leaflet components are wrapped into React components with flexibility for client-only execution.
Following this approach should help avoid errors related to window being undefined in a server environment. If you still encounter issues, double-check each component’s use and ensure no additional libraries are assuming a browser environment.