How to fix "Next.js: document is not defined"?
Last Updated :
15 Jul, 2024
The "document is not defined" error in next js happens because the document is a client-side browser object, which isn't available during server-side rendering. In this article, we'll explain why this error occurs and how to fix it.
Approach
To fix "Next.js: document is not defined" error, ensure code meant for server-side execution does not access browser-specific objects like document. Use conditional rendering or move problematic code to components that run only on the client-side using useEffect in functional components.
Steps to Setup a NextJS App
To create a new Next.js application, follow these steps:
Step 1: Create a demo Next.Js application running the following commands in the terminal.
npx create-next-app demoapp
cd demoapp
Step 2: In the terminal run the following command.
npm run dev
Project Structure:
demoapp project structureVerify the dependencies in package.json file.
"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "14.2.3"
}
When does this error occur?
The document object is part of the browser's Window interface and represents the DOM (Document Object Model) of the current web page. When Next.js renders pages on the server, there is no browser environment, so the document object is not defined. This results in the "document is not defined" error when code that references the document is executed on the server.
This error typically occurs in this scenario: Direct usage of document in the component's render method or lifecycle methods.
JavaScript
//index.js
import Head from "next/head";
// This code will throw an error during SSR
const MyComponent = () => {
const element = document.getElementById("some-id");
return <div>{element ? element.textContent : "No element"}</div>;
};
export default function Home() {
return (
<>
<Head>
<title>Create Next App</title>
<meta name="description"
content="Generated by create next app" />
<meta name="viewport"
content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<div style={{
minHeight: "100vh", backgroundColor: "white"
}}>
<MyComponent />
</div>
</main>
</>
);
}
document not defined errorSteps for Fixing Document not defined
- Use useEffect for Client-Side Code: The useEffect hook ensures that code accessing the document object only runs after the component mounts on the client side.
- Conditional Checks for document: Before accessing document, perform a conditional check to ensure it exists.
- Dynamic Imports for Client-Side Components: Dynamically import components that rely on the document object to prevent them from being rendered on the server.
- Avoid Direct DOM Manipulation in SSR: Ensure that DOM manipulation code is only executed in the browser environment.
- Utilize Conditional Rendering: Render components or perform actions conditionally based on the environment to ensure code runs in the correct context.
Conditional Checks for document
referencing to document during server-side rendering resulting in document not found error, so you need wrap the code that accesses document in a conditional check to ensure it only runs in the client-side environment.
JavaScript
// pages/index.js
import Head from "next/head";
import { useEffect, useState } from "react";
const MyComponent = () => {
const [elementContent, setElementContent] = useState("No element");
useEffect(() => {
const element = document.getElementById("some-id");
if (element) {
setElementContent(element.textContent);
}
}, []);
return (
<div>
<div id="some-id">Hello, geek!</div>
<h2>{elementContent} hi</h2>
</div>
);
};
export default function Home() {
const [docEnv, setDocEnv] = useState(false);
useEffect(() => {
if (typeof document !== "undefined") {
setDocEnv(true);
}
}, []);
return (
<>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<div
style={{
minHeight: "100vh",
backgroundColor: "white",
color: "black",
}}
>
{docEnv && <MyComponent />}
</div>
</main>
</>
);
}
The useEffect hook ensures that the code inside it runs only on the client side after the component has mounted. This means that the document object is available, allowing us to use document.getElementById to get an element's content and display it.
solved document not error code outputExplanation:
- Using useEffect for Client-Side Execution: The useEffect hook in MyComponent ensures that the code accessing the document object only runs after the component mounts. This avoids server-side rendering issues, as document is not defined on the server.
- Accessing and Displaying Element Content: Inside the useEffect, document.getElementById("some-id") fetches the div element with id="some-id". If the element exists, its text content is retrieved and stored in the elementContent state. The content is then displayed inside an h2 tag, allowing dynamic updates based on the element's content.
- Conditional Rendering: In the Home component, docEnv is set to true only if document is defined, ensuring that MyComponent is only rendered on the client side.'
Best practices to avoid document is not defined error
- Use useEffect to Run Client-Side Code: Ensure that code dependent on the document object is executed only on the client side by wrapping it in a useEffect hook.
- Check if document is Defined Before Accessing It: Perform a conditional check to confirm the existence of the document object.
- Use Dynamic Imports to Load Client-Side Components: Dynamically import components that rely on the document object to prevent them from being rendered on the server.
- Avoid Direct DOM Manipulation in SSR: Ensure that DOM manipulation code is only executed in the browser environment.
- Utilize Conditional Rendering: Render components or perform actions conditionally based on the environment to ensure code runs in the correct context.
Conclusion
"document is not defined" error in Next.js occurs because the document object is only available in the browser, not during server-side rendering. To avoid this error, use the useEffect hook to run client-side code, check if document is defined before accessing it, and use dynamic imports for client-side components. These practices ensure your application works seamlessly both on the server and in the browser.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read