Next.js Directory Structure
In Next.js, the directory structure is organized around the file-system routing mechanism, which means your files and folders directly determine your application's routes and page layout.

app/directory is used to store page and layout files and is the core directory in Next.js.public/is used to store public static asset files.styles/is used to store style files.node_modules/Stores all of the project's dependency packages.package.jsonis the project's configuration file, containing project dependencies and script commands.next.config.jsis used to customize the Next.js configuration.
Root Directory
The root directory usually contains some basic configuration files and dependencies. Common files and folders are as follows:
my-next-app/
├── app/ # 存放应用页面和布局文件
├── public/ # 存放静态资源文件(如图片、字体)
├── styles/ # 存放样式文件(CSS、Sass等)
├── node_modules/ # 存放项目依赖
├── package.json # 项目配置文件
├── next.config.js # Next.js 配置文件(可选)
└── .gitignore # Git 忽略文件
app/: This directory is the new way in Next.js to organize page and layout files. All routes and pages are created based on this directory.public/: This directory stores public static files, such as images, fonts, favicons, and so on. You can access these files directly via URL.styles/: Stores your CSS, Sass, or other style files.
app/ Directory
The app/ directory is the core of Next.js, storing pages, layouts, and other related files.
The following are some common structures of the app/ directory:
app/
├── layout.tsx # 根布局文件
├── page.tsx # 根页面
└── [slug]/ # 动态路由目录
├── page.tsx # 动态页面
└── layout.tsx # 特定页面的布局
layout.tsx: In this file, the root layout or page layout is defined, usually used to set the global structure (such asandtags). All child pages will be nested within this layout.page.tsx: Each page component should have apage.tsxfile. This file represents the page content for that route. For example,app/page.tsxcorresponds to the root path (/) page.- Dynamic Route: Next.js supports dynamic routing; the name of a dynamic route directory is wrapped in square brackets, such as
[slug]. This means you can load different pages based on different parts of the URL. For example,[slug]/page.tsxwill render based onslugparameters.
public/ Directory
This directory is used to store static assets; it lets you expose resources such as images, fonts, and icons in the application and load them by directly accessing the file path.
public/
├── images/ # 存放图片文件
├── fonts/ # 存放字体文件
└── favicon.ico # 网站图标
- Files can be accessed via
/images/and other paths. For example,public/images/logo.pngcan be used viahttp://localhost:3000/images/logo.png.
styles/ Directory
In the styles/ directory, you can organize and manage your style files. Next.js supports CSS Modules, global styles, and other CSS solutions.
styles/
├── Home.module.css # CSS 模块化样式
└── globals.css # 全局样式
Home.module.css: This is a CSS Module file. By default, Next.js automatically uses CSS Modules for each component, ensuring that styles are scoped to that component only.globals.css: Stores global styles, typically used to set global fonts, colors, and so on.
node_modules/ Directory
This directory stores all the project's dependency packages. It is automatically generated by npm or yarn based on the dependency information in the package.json file and requires no manual operation.
package.json File
package.json is the core configuration file of the project, recording the project's dependencies, scripts, and other metadata. You can define commands here for starting, building, and developing.
Example
{
"scripts"
:
{
"dev"
:
"next dev"
,
"build"
:
"next build"
,
"start"
:
"next start"
,
"lint"
:
"next lint"
}
}
next.config.js File (Optional)
This is an optional configuration file used to configure Next.js's behavior. For example, you can set static file optimization, routing rules, and Webpack configuration in this file.
Example
module.
exports
=
{
reactStrictMode
:
true
,
}
Project Structure Example
The following is an example of a typical Next.js project structure:
Example: Next.js Project Structure
my-next-app
/
├── app
/
│ ├── layout.js
//
Root Layout
│ ├── page.js
//
Home
│ ├── about
/
│ │ ├── layout.js
//
/
about Layout
│ │ └── page.js
//
/
about Page
│ ├── blog
/
│ │ ├── layout.js
//
/
blog Layout
│ │ ├── page.js
//
/
blog List Page
│ │ └──
[
slug
]
/
│ │ └── page.js
//
/
blog
/
:slug Detail Page
│ └── dashboard
/
│ ├── layout.js
//
/
dashboard Layout
│ └── page.js
//
/
dashboard Page
├── public
/
│ ├── logo.png
//
Static Assets
│ └── favicon.ico
//
Favicon
├── node_modules
/
//
Project Dependencies
├── package.json
//
Project Configuration File
├── next.config.js
//
Next.js Configuration File
└── .next
/
//
Build Output Directory
Comparison of the app Directory and the Traditional pages Directory
Next.js 13 introduced the new app directory, which coexists with the traditional pages directory and provides more flexible routing and layout organization.
| Feature | pages directory |
app directory |
|---|---|---|
| Route Definition | File-System Routing | Folder Routing |
| layout management | Manual Implementation | Nested Layout |
| Data Fetching | getStaticProps etc. |
Server Components + async/await |
| File Naming | Free Naming | Must be named page.js |
| Special Files | _app.js, _document.js etc. |
layout.js, loading.js etc. |
| Performance optimization | Manual Optimization | Default Optimization |
| Compatibility | Fully Compatible | In Beta, requires manual enabling |