Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Next.js Directory Structure

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.

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/ 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       # 特定页面的布局

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          # 网站图标

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          # 全局样式

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