---
title: "安装"
description: "在你的 Nuxt 应用中开始使用 Nuxt Content v3。"
canonical_url: "https://nuxt-content.zhcndoc.com/docs/getting-started/installation"
---
# 安装

> 在你的 Nuxt 应用中开始使用 Nuxt Content v3。

## 安装包

选择你喜欢的包管理器来安装 Nuxt Content v3：

<code-group>

```bash [pnpm]
pnpm add @nuxt/content
```

```bash [yarn]
yarn add @nuxt/content
```

```bash [npm]
npm install @nuxt/content
```

```bash [bun]
bun add @nuxt/content
```

</code-group>

## 注册模块

在你的 `nuxt.config.ts` 文件中添加 Nuxt Content 模块：

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['@nuxt/content']
})
```

## 自动安装

使用 `create-nuxt` CLI 创建新 Nuxt 项目时，可以直接从交互式模块选择器中选择 `@nuxt/content`。这将自动为你安装并注册该模块。

<code-group>

```bash [npm]
npm create nuxt <project-name>
```

```bash [yarn]
yarn create nuxt <project-name>
```

```bash [pnpm]
pnpm create nuxt <project-name>
```

```bash [bun]
bun create nuxt <project-name>
```

```bash [deno]
deno -A npm:create-nuxt@latest <project-name>
```

</code-group>

<note color="warning">

当你在 Node.js 中运行项目时，Nuxt Content 会询问你使用哪个数据库连接器。<br />


你可以选择安装 `better-sqlite3` 或 `sqlite3` 包。

<br />

如果你不想安装任何包，可以使用 Node.js @v22.5.0 或更新版本中的原生 SQLite。<br />


请参阅 [`experimental.sqliteConnector`](https://nuxt-content.zhcndoc.com/docs/getting-started/configuration#experimentalsqliteconnector) [配置](https://nuxt-content.zhcndoc.com/docs/getting-started/configuration#experimentalsqliteconnector)。

</note>

<note color="warning">

如果你使用 **pnpm v10+**，默认不会执行依赖的构建脚本。

<br />

由于 `better-sqlite3` 和 `sqlite3` 依赖安装后运行的构建步骤来生成本地绑定文件，<br />


你可能会遇到以下错误：

<br />

`Could not locate the bindings file`

<br />

解决方法是通过运行以下命令批准所需的构建脚本：

<code-group>

```bash [pnpm]
pnpm approve-builds
```

</code-group>

或者，如果你需要无交互的设置（例如在 CI 中），<br />


可以通过在项目根目录的 `package.json` 中添加如下配置，显式允许本地构建：

```json [package.json]
{
  "pnpm": {
    "onlyBuiltDependencies": [
      "better-sqlite3",
      "sqlite3"
    ]
  }
}
```

你可能还需要清空 `node_modules`，或者如果初始安装没有完成构建，则显式重新构建：

<code-group>

```bash [pnpm]
pnpm rebuild better-sqlite3 sqlite3
```

</code-group>
</note>

## 创建你的第一个集合

在项目根目录创建一个 `content.config.ts` 文件：

```ts [content.config.ts]
import { defineContentConfig, defineCollection } from '@nuxt/content'

export default defineContentConfig({
  collections: {
    content: defineCollection({
      type: 'page',
      source: '**/*.md'
    })
  }
})
```

该配置创建了一个默认的 `content` 集合，用于处理项目 `content` 文件夹内的所有 Markdown 文件。你可以根据需要自定义集合配置。

<tip>

`type: page` 表示内容文件和网站页面之间是一对一的关系。

</tip>

<note to="https://nuxt-content.zhcndoc.com/docs/collections/define">

了解更多，请查看我们的 **集合指南**。

</note>

## 创建你的第一个 Markdown 页面

在项目根目录创建 `content/index.md` 文件：

```md [content/index.md]
# 我的第一个页面

这里是一段内容。
```

查看更多关于编写 [Markdown 页面](https://nuxt-content.zhcndoc.com/docs/files/markdown) 的信息。

## Display Your Page

Create the `pages/index.vue` file and display the page content:

```vue [pages/index.vue]
<script setup lang="ts">
const { data: home } = await useAsyncData(() => queryCollection('content').path('/').first())

useSeoMeta({
  title: home.value?.title,
  description: home.value?.description
})
</script>

<template>
  <ContentRenderer v-if="home" :value="home" />
  <div v-else>Home not found</div>
</template>
```

<note icon="i-lucide-info">

If you install Nuxt Content in a new Nuxt project and the project does not have a `pages` directory, you also need to update the `app.vue` file by adding the `NuxtPage` component to allow pages to be rendered. (If your project already has pages, you do not need to do this.)

```vue [app.vue]
<template>
  <NuxtLayout>
    <NuxtPage />
  </NuxtLayout>
</template>
```

</note>

<tip icon="i-lucide-rocket">

That’s it! You’ve created your first Nuxt Content page.

</tip>


## Sitemap

See the full [sitemap](https://nuxt-content.zhcndoc.com/sitemap.md) for all pages.
