基礎入門
CoreSetup
安裝指南
跟隨本指南,在幾分鐘內建立一個現代化、高效能的 Nuxt 4 應用程式。我們強烈建議使用 pnpm 以獲得最佳體驗。
環境準備
Node.js
Runtime Environment
Nuxt 4 需要 Node.js 18.0.0 或更高版本。
建議使用 LTS (Long Term Support) 版本以確保穩定性。
建議使用 LTS (Long Term Support) 版本以確保穩定性。
Package Manager
Dependency Management
雖然 npm 與 yarn 也能運作,但我們強烈推薦使用 pnpm。
它能大幅節省磁碟空間並加快安裝速度。
它能大幅節省磁碟空間並加快安裝速度。
為什麼選擇 Nuxt 4?
Nuxt 4 引入了 compatibilityVersion: 4,這是一個重大的架構升級。 它將應用程式源碼集中於 app/ 目錄,讓專案根目錄更加乾淨。
Project Structure Evolution
Nuxt 3 (Legacy)
components/
layouts/
pages/
public/
server/
...還有更多散落在根目錄
Nuxt 4 (Modern)
- app/
- components/
- layouts/
- pages/
- server/
- nuxt.config.ts
初始化專案
Terminal
1. 建立專案
$ pnpm dlx nuxi@latest init <project-name>
2. 進入目錄
$ cd <project-name>
3. 安裝依賴
$ pnpm install
關鍵設定
為了啟用 Nuxt 4 的目錄結構,你必須在 nuxt.config.ts 中明確宣告。
nuxt.config.ts
export default defineNuxtConfig({
future: {
compatibilityVersion: 4
}
})為什麼要手動設定? 目前 Nuxt 4 仍處於過渡期。這個設定告訴 Nuxt 掃描
app/ 目錄而非根目錄,讓你能提早享受未來的專案架構。 啟動開發
一切就緒!現在啟動開發伺服器,Nuxt 會自動開啟瀏覽器並啟用 HMR (熱模組替換)。
$ pnpm dev
預設網址: http://localhost:3000
Browser Preview
Welcome to Nuxt!
Your application is running.