Skip to content

安装

¥Installation

系统要求

¥System requirements

开始之前,请确保你的开发环境满足以下要求:

¥Before you begin, make sure your development environment meets the following requirements:

  • 最低 Node.js 版本:20.9

    ¥Minimum Node.js version: 20.9

  • 操作系统:macOS、Windows(包括 WSL)和 Linux。

    ¥Operating systems: macOS, Windows (including WSL), and Linux.

支持的浏览器

¥Supported browsers

Next.js 以零配置支持现代浏览器。

¥Next.js supports modern browsers with zero configuration.

  • Chrome 111+

  • Edge 111+

  • Firefox 111+

  • Safari 16.4+

了解更多关于 浏览器支持 的信息,包括如何配置 polyfill 和针对特定浏览器。

¥Learn more about browser support, including how to configure polyfills and target specific browsers.

使用 CLI 创建

¥Create with the CLI

创建新 Next.js 应用的最快方法是使用 create-next-app,它会为你自动设置一切。要创建项目,请运行:

¥The quickest way to create a new Next.js app is using create-next-app, which sets up everything automatically for you. To create a project, run:

bash
npx create-next-app@latest

安装时,你将看到以下提示:

¥On installation, you'll see the following prompts:

txt
What is your project named? my-app
Would you like to use the recommended Next.js defaults?
    Yes, use recommended defaults - TypeScript, ESLint, Tailwind CSS, App Router, Turbopack
    No, reuse previous settings
    No, customize settings - Choose your own preferences

如果你选择使用 customize settings,你将看到以下提示:

¥If you choose to customize settings, you'll see the following prompts:

txt
Would you like to use TypeScript? No / Yes
Which linter would you like to use? ESLint / Biome / None
Would you like to use React Compiler? No / Yes
Would you like to use Tailwind CSS? No / Yes
Would you like your code inside a `src/` directory? No / Yes
Would you like to use App Router? (recommended) No / Yes
Would you like to customize the import alias (`@/*` by default)? No / Yes
What import alias would you like configured? @/*

出现提示后,create-next-app 将以你的项目名称创建一个文件夹并安装所需的依赖。

¥After the prompts, create-next-app will create a folder with your project name and install the required dependencies.

手动安装

¥Manual installation

要手动创建新的 Next.js 应用,请安装所需的包:

¥To manually create a new Next.js app, install the required packages:

bash
pnpm i next@latest react@latest react-dom@latest
bash
npm i next@latest react@latest react-dom@latest
bash
yarn add next@latest react@latest react-dom@latest
bash
bun add next@latest react@latest react-dom@latest

需要了解:App Router 使用内置的 React 预览版发布,其中包含所有稳定的 React 19 更改以及正在框架中验证的新功能。Pages Router 使用你在 package.json 中安装的 React 版本。

¥Good to know: The App Router uses React canary releases built-in, which include all the stable React 19 changes, as well as newer features being validated in frameworks. The Pages Router uses the React version you install in package.json.

然后,将以下脚本添加到你的 package.json 文件:

¥Then, add the following scripts to your package.json file:

这些脚本涉及开发应用的不同阶段:

¥These scripts refer to the different stages of developing an application:

  • next dev:使用 Turbopack(默认打包工具)启动开发服务器。

    ¥next dev: Starts the development server using Turbopack (default bundler).

  • next build:构建用于生产环境的应用。

    ¥next build: Builds the application for production.

  • next start:启动生产服务器。

    ¥next start: Starts the production server.

  • eslint:运行 ESLint。

    ¥eslint: Runs ESLint.

Turbopack 现在是默认打包工具。要使用 Webpack,请运行 next dev --webpacknext build --webpack。请参阅 Turbopack 文档 获取配置详情。

¥Turbopack is now the default bundler. To use Webpack run next dev --webpack or next build --webpack. See the Turbopack docs for configuration details.

创建 pages 目录

¥Create the pages directory

Next.js 使用文件系统路由,这意味着应用中的路由由你构建文件的方式决定。

¥Next.js uses file-system routing, which means the routes in your application are determined by how you structure your files.

在项目根目录下创建一个 pages 目录。然后,在 pages 文件夹中添加一个 index.tsx 文件。这将是你的主页 (/):

¥Create a pages directory at the root of your project. Then, add an index.tsx file inside your pages folder. This will be your home page (/):

tsx
export default function Page() {
  return <h1>Hello, Next.js!</h1>
}

接下来,在 pages/ 中添加一个 _app.tsx 文件来定义全局布局。了解有关 自定义应用文件 的更多信息。

¥Next, add an _app.tsx file inside pages/ to define the global layout. Learn more about the custom App file.

tsx
import type { AppProps } from 'next/app'

export default function App({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />
}

最后,在 pages/ 中添加一个 _document.tsx 文件,以控制来自服务器的初始响应。了解有关 自定义文档文件 的更多信息。

¥Finally, add a _document.tsx file inside pages/ to control the initial response from the server. Learn more about the custom Document file.

tsx
import { Html, Head, Main, NextScript } from 'next/document'

export default function Document() {
  return (
    <Html>
      <Head />
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  )
}

创建 public 文件夹(可选)

¥Create the public folder (optional)

在项目根目录创建一个 public 文件夹,用于存储静态资源,例如图片、字体等。然后,你的代码可以从基本 URL(/)开始引用 public 中的文件。

¥Create a public folder at the root of your project to store static assets such as images, fonts, etc. Files inside public can then be referenced by your code starting from the base URL (/).

然后,你可以使用根路径 (/) 引用这些资源。例如,public/profile.png 可以引用为 /profile.png

¥You can then reference these assets using the root path (/). For example, public/profile.png can be referenced as /profile.png:

tsx
import Image from 'next/image'

export default function Page() {
  return <Image src="/profile.png" alt="Profile" width={100} height={100} />
}

运行开发服务器

¥Run the development server

  1. 运行 npm run dev 以启动开发服务器。

    ¥Run npm run dev to start the development server.

  2. 访问 http://localhost:3000 查看你的应用。

    ¥Visit http://localhost:3000 to view your application.

  3. 编辑 pages/index.tsx 文件并保存,以便在浏览器中查看更新后的结果。

    ¥Edit the pages/index.tsx file and save it to see the updated result in your browser.

设置 TypeScript

¥Set up TypeScript

最低 TypeScript 版本:v5.1.0

¥Minimum TypeScript version: v5.1.0

Next.js 带有内置 TypeScript 支持。要将 TypeScript 添加到项目中,请将文件重命名为 .ts / .tsx,然后运行 ​​next dev。Next.js 将自动安装必要的依赖,并添加包含推荐配置选项的 tsconfig.json 文件。

¥Next.js comes with built-in TypeScript support. To add TypeScript to your project, rename a file to .ts / .tsx and run next dev. Next.js will automatically install the necessary dependencies and add a tsconfig.json file with the recommended config options.

请参阅 TypeScript 参考 页了解更多信息。

¥See the TypeScript reference page for more information.

设置代码检查

¥Set up linting

Next.js 支持使用 ESLint 或 Biome 进行代码检查。选择一个代码检查工具,并通过 package.json 脚本直接运行。

¥Next.js supports linting with either ESLint or Biome. Choose a linter and run it directly via package.json scripts.

  • 使用 ESLint(综合规则):

    ¥Use ESLint (comprehensive rules):

  • 或者使用 Biome(快速代码检查器 + 格式化器):

    ¥Or use Biome (fast linter + formatter):

如果你的项目之前使用了 next lint,请使用 codemod 将脚本迁移到 ESLint CLI:

¥If your project previously used next lint, migrate your scripts to the ESLint CLI with the codemod:

bash
npx @next/codemod@canary next-lint-to-eslint-cli .

如果你使用 ESLint,请创建一个显式配置(推荐使用 eslint.config.mjs)。ESLint 同时支持 旧版 .eslintrc.* 和新版 eslint.config.mjs 格式。请参阅 ESLint API 参考 获取推荐配置。

¥If you use ESLint, create an explicit config (recommended eslint.config.mjs). ESLint supports both the legacy .eslintrc.* and the newer eslint.config.mjs formats. See the ESLint API reference for a recommended setup.

需要了解:从 Next.js 16 开始,next build 不再自动运行代码检查器。或者,你可以通过 NPM 脚本运行代码检查器。

¥Good to know: Starting with Next.js 16, next build no longer runs the linter automatically. Instead, you can run your linter through NPM scripts.

请参阅 ESLint 插件 页了解更多信息。

¥See the ESLint Plugin page for more information.

设置绝对导入和模块路径别名

¥Set up Absolute Imports and Module Path Aliases

Next.js 内置支持 tsconfig.jsonjsconfig.json 文件的 "paths""baseUrl" 选项。

¥Next.js has in-built support for the "paths" and "baseUrl" options of tsconfig.json and jsconfig.json files.

这些选项允许你将项目目录别名为绝对路径,从而使导入模块更加轻松简洁。例如:

¥These options allow you to alias project directories to absolute paths, making it easier and cleaner to import modules. For example:

要配置绝对导入,请将 baseUrl 配置选项添加到你的 tsconfig.jsonjsconfig.json 文件中。例如:

¥To configure absolute imports, add the baseUrl configuration option to your tsconfig.json or jsconfig.json file. For example:

除了配置 baseUrl 路径之外,你还可以使用 "paths" 选项来配置 "alias" 模块路径。

¥In addition to configuring the baseUrl path, you can use the "paths" option to "alias" module paths.

例如,以下配置将 @/components/* 映射到 components/*

¥For example, the following configuration maps @/components/* to components/*:

每个 "paths" 都相对于 baseUrl 位置。

¥Each of the "paths" are relative to the baseUrl location.