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
Verify the dependencies in package.json file.
"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "14.2.3"
}
How to fix “Next.js: document is not defined”?
Next.js is a powerful React framework that allows developers to create server-side rendered (SSR) and static web applications. A common error encountered while working with Next.js is the “document is not defined” error. This error 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.
Table of Content
- When does this error occur?
- Fixing Document not defined
- Detailed Code Explanation
- Best practices to avoid document is not defined error
- Conclusion