Next.js Project Structure
A typical Next.js project contains specific files and directories. Understanding their roles helps you better organize and develop your application.
Top-Level Files and Directories
When you create a new project using create-next-app, you'll see the following basic structure:
my-next-app/
├── .next/
├── node_modules/
├── pages/
│ ├── _app.js
│ ├── api/
│ │ └── hello.js
│ └── index.js
├── public/
│ ├── favicon.ico
│ └── vercel.svg
├── styles/
│ ├── globals.css
│ └── Home.module.css
├── .eslintrc.json
├── .gitignore
├── next.config.js
├── package.json
├── README.md
└── yarn.lockLet's understand the purpose of each file and directory.
.next
This is the Next.js build directory, containing compiled code, cache, and static assets. You don't need to manually modify anything in this directory; Next.js automatically manages it during build and development.
node_modules
Contains all project dependencies. When you run npm install or yarn, dependencies are downloaded to this directory.
pages
This is one of the most important directories in Next.js, used for file system routing.
- Page Routing: Each React component in the
pagesdirectory maps to a route. For example,pages/about.jscorresponds to the/aboutroute, andpages/posts/first-post.jscorresponds to/posts/first-post. index.js: Theindex.jsfile in each directory serves as the root route for that directory. For example,pages/index.jsis the homepage (/), andpages/posts/index.jsis the/postsroute._app.js: A special component used to initialize all pages. You can add global styles, layouts, or state management here._document.js: Another special component used to customize the HTML document structure returned by the server. Typically used to add attributes to<html>and<body>.apiDirectory: Files inpages/apiare mapped as API routes. For example,pages/api/user.jscreates a/api/userAPI endpoint.
public
Used to store static assets such as images, fonts, and icons. Files in the public directory can be accessed directly from the application's root path. For example, public/my-image.png can be accessed via /my-image.png.
styles
Contains the application's style files. Next.js supports multiple styling solutions:
- Global Styles: The
styles/globals.cssfile is used for global styles and needs to be imported inpages/_app.js. - CSS Modules: Files ending with
.module.cssare CSS Modules. They generate unique class names to avoid style conflicts. For example,styles/Home.module.css.
components (Optional)
Although create-next-app doesn't create a components directory by default, it's usually recommended to create one for storing reusable React components such as navigation bars, buttons, and cards. This helps keep the code clean and maintainable.
Configuration Files
.eslintrc.json: ESLint configuration file for code standards and error checking..gitignore: Tells Git which files or directories shouldn't be included in version control, such as.nextandnode_modules.next.config.js: The main Next.js configuration file. You can configure environment variables, redirects, proxies, Webpack, and other advanced features here.package.json: Project metadata file containing project name, version, dependencies, and script commands.README.md: Project documentation, typically containing project introduction, installation, and usage instructions.
App Router (Next.js 13+)
Starting from Next.js 13, a new App Router was introduced, using the app directory instead of pages. The App Router brings more powerful layout features and server components.
If your project uses the App Router, the project structure will be somewhat different:
my-next-app/
├── app/
│ ├── layout.js
│ └── page.js
...appDirectory: Stores App Router pages, layouts, and components.layout.js: Defines shared UI layouts.page.js: Defines page UI content.
This tutorial is mainly based on the pages directory, but understanding the App Router is very important for learning the latest Next.js.
Summary
Understanding the Next.js project structure is the foundation for efficient development. By properly organizing files and directories, you can build clear, maintainable web applications.
Previous Chapter: Next.js Installation | Next Chapter: Next.js Project Explanation