安装与创建项目

创建一个 Nuxt 4 项目只要一条命令。本章介绍环境要求、官方 CLI、手动安装,以及后面一直会用到的目录结构。

环境要求

  • Node.js 20.x 或更新版本,建议使用偶数 LTS(20、22、……)。
  • 终端 + 编辑器:VS Code + Vue (Volar) 插件或 WebStorm 都行。
  • Windows 用户建议:用 WSL,并用 127.0.0.1 代替 localhost,可避免 DNS 慢和 HMR 抖动。

用 CLI 初始化

# pnpm
pnpm create nuxt@latest my-app

# npm
npm create nuxt@latest my-app

# bun
bun create nuxt@latest my-app

CLI 会询问包管理器、Linter、CSS 方案等。选完后:

cd my-app
pnpm install
pnpm dev -o   # -o 会自动打开浏览器

访问 http://localhost:3000 就能看到默认欢迎页。

手动安装

想搞清楚底层发生了什么,就从零开始:

mkdir my-app && cd my-app
pnpm init
pnpm add -D nuxt

package.json 中加入脚本:

{
  "scripts": {
    "dev": "nuxt dev",
    "build": "nuxt build",
    "preview": "nuxt preview"
  }
}

新建 app.vue

<template>
  <div>Hello Nuxt 4</div>
</template>

pnpm dev 启动后就能访问 http://localhost:3000,其余细节 Nuxt 会自动处理。

目录结构

Nuxt 4 推荐的目录布局:

my-app/
├── app/
│   ├── app.vue              # 根组件
│   ├── pages/               # 文件式路由
│   ├── layouts/             # 布局
│   ├── components/          # 自动导入的组件
│   ├── composables/         # 自动导入的组合式函数
│   ├── utils/               # 自动导入的工具
│   ├── middleware/          # 路由中间件
│   ├── plugins/             # 客户端/服务端插件
│   └── assets/              # 需要 Vite 处理的资源
├── public/                  # 原样托管到 /
├── server/
│   ├── api/                 # /api/* 接口
│   ├── middleware/          # 服务端中间件
│   └── plugins/             # Nitro 插件
├── nuxt.config.ts           # 框架配置
├── tsconfig.json            # 从 .nuxt/ 继承
└── package.json

两点特别要记住:

  • app/ 下的所有内容都被自动导入 —— 自己的组件与 composable 都不需要写 import
  • server/ 跑在 Nitro 上,不是 Vite。服务端接口都写在这里。

开发服务器

pnpm dev              # 启动 dev 服务器
pnpm dev -o           # 自动打开浏览器
pnpm dev --port 4000  # 换端口

HMR、TypeScript、Vite、Nitro dev server 全部自动配好。

nuxt.config.ts 基本用法

最小 nuxt.config.ts 一般会打开 DevTools、注册模块:

export default defineNuxtConfig({
  devtools: { enabled: true },
  modules: ['@pinia/nuxt', '@nuxtjs/tailwindcss'],
})

这点配置就足够开工了。下一章会细讲环境变量覆盖、runtimeConfigapp.config 区别、TypeScript 集成等内容,但到这一步已经可以写页面。

写下第一页

app.vue 改成:

<template>
  <NuxtLayout>
    <NuxtPage />
  </NuxtLayout>
</template>

新建 app/pages/index.vue

<template>
  <section>
    <h1>Hello Nuxt 4</h1>
    <p>30 秒体验文件式路由。</p>
  </section>
</template>

路由完全由 app/pages/ 生成,不需要任何路由表。

安裝與建立專案

建立一個 Nuxt 4 專案只需一條指令。本章介紹環境需求、官方 CLI、手動安裝,以及往後會一直用到的目錄結構。

環境需求

  • Node.js 20.x 或更新版本,建議使用偶數 LTS(20、22、……)。
  • 終端機 + 編輯器:VS Code + Vue (Volar) 擴充套件或 WebStorm 皆可。
  • Windows 使用者建議:採用 WSL,並以 127.0.0.1 取代 localhost,可避免 DNS 慢與 HMR 抖動。

以 CLI 初始化

# pnpm
pnpm create nuxt@latest my-app

# npm
npm create nuxt@latest my-app

# bun
bun create nuxt@latest my-app

CLI 會詢問套件管理器、Linter、CSS 方案等。選擇完畢後:

cd my-app
pnpm install
pnpm dev -o   # -o 自動開啟瀏覽器

開啟 http://localhost:3000 即可看到預設歡迎頁。

手動安裝

想了解底層運作,就從零做起:

mkdir my-app && cd my-app
pnpm init
pnpm add -D nuxt

package.json 新增指令:

{
  "scripts": {
    "dev": "nuxt dev",
    "build": "nuxt build",
    "preview": "nuxt preview"
  }
}

新建 app.vue

<template>
  <div>Hello Nuxt 4</div>
</template>

執行 pnpm dev 即可造訪 http://localhost:3000,其餘事項 Nuxt 會自動處理。

目錄結構

Nuxt 4 推薦的目錄:

my-app/
├── app/
│   ├── app.vue              # 根元件
│   ├── pages/               # 檔案式路由
│   ├── layouts/             # 佈局
│   ├── components/          # 自動匯入的元件
│   ├── composables/         # 自動匯入的組合式函式
│   ├── utils/               # 自動匯入的工具
│   ├── middleware/          # 路由中介層
│   ├── plugins/             # 客戶端/伺服器外掛
│   └── assets/              # 由 Vite 處理的資源
├── public/                  # 原樣由 / 提供
├── server/
│   ├── api/                 # /api/* 端點
│   ├── middleware/          # 伺服器中介層
│   └── plugins/             # Nitro 外掛
├── nuxt.config.ts           # 框架設定
├── tsconfig.json            # 由 .nuxt/ 繼承
└── package.json

兩點必須記住:

  • app/ 下的內容皆自動匯入 —— 自家元件與 composable 皆毋須手動 import
  • server/ 執行於 Nitro 而非 Vite,伺服器端點都寫於此。

開發伺服器

pnpm dev              # 啟動 dev 伺服器
pnpm dev -o           # 自動開啟瀏覽器
pnpm dev --port 4000  # 改變連接埠

HMR、TypeScript、Vite、Nitro dev server 全數自動就位。

nuxt.config.ts 基本用法

最簡 nuxt.config.ts 通常會啟用 DevTools 並註冊模組:

export default defineNuxtConfig({
  devtools: { enabled: true },
  modules: ['@pinia/nuxt', '@nuxtjs/tailwindcss'],
})

這樣便足以開工。下一章會詳述環境變數覆寫、runtimeConfigapp.config 的分別、TypeScript 整合等細節,不過目前已可開始寫頁面。

寫下第一頁

app.vue 改為:

<template>
  <NuxtLayout>
    <NuxtPage />
  </NuxtLayout>
</template>

新建 app/pages/index.vue

<template>
  <section>
    <h1>Hello Nuxt 4</h1>
    <p>30 秒體驗檔案式路由。</p>
  </section>
</template>

路由完全由 app/pages/ 產生,毋須任何路由表。

Installation

Scaffolding a new Nuxt 4 project takes one command. This chapter covers the system prerequisites, the official CLI, the manual path, and the folder layout you'll live in for the rest of the book.

Prerequisites

  • Node.js 20.x or newer. Prefer an even-numbered LTS release (20, 22, …).
  • A terminal and a text editor. VS Code + the Vue (Volar) extension or WebStorm both work great.
  • Windows users: WSL + 127.0.0.1 avoids slow DNS and flaky HMR.

Scaffold with the CLI

# pnpm
pnpm create nuxt@latest my-app

# npm
npm create nuxt@latest my-app

# bun
bun create nuxt@latest my-app

The CLI asks you to choose a package manager, linter, CSS framework, and so on. Pick what you want, then:

cd my-app
pnpm install
pnpm dev -o   # -o opens the browser

Open http://localhost:3000 and you should see the default welcome page.

Manual install

If you want to understand what's happening, start from scratch:

mkdir my-app && cd my-app
pnpm init
pnpm add -D nuxt

Then add a minimal package.json:

{
  "scripts": {
    "dev": "nuxt dev",
    "build": "nuxt build",
    "preview": "nuxt preview"
  }
}

Create app.vue:

<template>
  <div>Hello Nuxt 4</div>
</template>

pnpm dev now prints http://localhost:3000. Nuxt generates the rest on its own.

Project structure

Here is the canonical Nuxt 4 layout:

my-app/
├── app/
│   ├── app.vue              # Root component
│   ├── pages/               # File-based routes
│   ├── layouts/             # Layout wrappers
│   ├── components/          # Auto-imported components
│   ├── composables/         # Auto-imported composables
│   ├── utils/               # Auto-imported helpers
│   ├── middleware/          # Route middleware
│   ├── plugins/             # Client/server plugins
│   └── assets/              # Processed by Vite (styles, images…)
├── public/                  # Served as-is from /
├── server/
│   ├── api/                 # /api/* handlers
│   ├── middleware/          # Server middleware
│   └── plugins/             # Nitro plugins
├── nuxt.config.ts           # Framework config
├── tsconfig.json            # Auto-extended from .nuxt/
└── package.json

Two things to notice:

  • Everything under app/ is auto-imported — no manual import for your own components or composables.
  • server/ runs on Nitro, not Vite. Route handlers live here.

Development server

pnpm dev       # start the dev server
pnpm dev -o    # and open a browser
pnpm dev --port 4000

HMR, TypeScript, Vite transform, Nitro dev server — all wired up.

nuxt.config.ts essentials

A minimal nuxt.config.ts usually enables Nuxt DevTools and registers a few modules:

export default defineNuxtConfig({
  devtools: { enabled: true },
  modules: ['@pinia/nuxt', '@nuxtjs/tailwindcss'],
})

That's enough to be productive. The next chapter drills into configuration — env overrides, runtimeConfig vs app.config, TypeScript integration — but at this point you can already build pages.

Your first page

Delete the contents of app.vue and add:

<template>
  <NuxtLayout>
    <NuxtPage />
  </NuxtLayout>
</template>

Create app/pages/index.vue:

<template>
  <section>
    <h1>Hello Nuxt 4</h1>
    <p>File-based routing in 30 seconds.</p>
  </section>
</template>

Routes are generated from app/pages/. No router config required.