# 命令行接口

> Source: https://strapi.nodejs.cn/cms/installation/cli

🌐 Installing from CLI

Strapi CLI 是使用 `npx create-strapi@latest` 在本地安装 Strapi 的最快方式，并且可以通过命令行选项配置 TypeScript、数据库（`--dbclient`、`--dbhost` 等）以及其他安装偏好设置。

🌐 The Strapi CLI is the fastest way to install Strapi locally using `npx create-strapi@latest`, with options to configure TypeScript, databases (`--dbclient`, `--dbhost`, etc.), and other setup preferences via command-line flags.

Strapi CLI（命令行接口）安装脚本是让 Strapi 本地运行的最快方式。以下指南是 Strapi 最推荐的安装选项。

🌐 Strapi CLI (Command Line Interface) installation scripts are the fastest way to get Strapi running locally. The following guide is the installation option most recommended by Strapi.

## 准备安装 {#preparing-the-installation}

🌐 Preparing the installation

在安装 Strapi 之前，你的计算机上必须安装以下要求：

🌐 Before installing Strapi, the following requirements must be installed on your computer:

- [Node.js](https://nodejs.cn)：仅支持 [Active LTS or Maintenance LTS versions](https://nodejs.cn/about/previous-releases) （目前为 `v22`、`v24` 和 `v26`）。Node 的奇数版本发布，称为 Node.js 的“当前”版本，不受支持（例如 v23、v25）。
- 你首选的 Node.js 包管理器：
    - [npm](https://npm.nodejs.cn/cli/v6/commands/npm-install) (`v6`及以上)
    - [pnpm](https://pnpm.nodejs.cn/) （在 Strapi Cloud 上， [Corepack](https://nodejs.cn/api/corepack.html)  与项目的 `package.json` `packageManager` 字段中固定的 pnpm 版本匹配，如果没有固定，则使用 Corepack 打包的默认版本）
- [Python](https://www.python.org/downloads/) （如果使用 SQLite 数据库）
- 受支持的网络浏览器：管理面板的目标浏览器是匹配默认 [Browserslist](https://github.com/browserslist/browserslist) 查询的浏览器：`last 3 major versions`、`Firefox ESR`、`last 2 Opera versions` 和 `not dead`。请参见 [browsersl.ist](https://browsersl.ist/#q=last+3+major+versions%2C+Firefox+ESR%2C+last+2+Opera+versions%2C+not+dead) 了解当前的覆盖矩阵。项目可以在项目根目录下通过 Browserslist 配置覆盖这些默认设置。

任何 Strapi 项目也需要一个支持的数据库：

| 数据库 | 推荐 | 最低 |
|------------|-------------|---------|
| MySQL | 8.4 | 8.0 |
| MariaDB | 11.4 | 10.3 |
| PostgreSQL | 17.0 | 14.0 |
| SQLite | 3 | 3 |

Strapi 不支持 MongoDB（或任何 NoSQL 数据库），也不支持任何“云原生”数据库（例如 Amazon Aurora、Google Cloud SQL 等）。

🌐 Strapi does not support MongoDB (or any NoSQL databases), nor does it support any "Cloud Native" databases (e.g., Amazon Aurora, Google Cloud SQL, etc.).

## 创建一个 Strapi 项目 {#creating-a-strapi-project}

🌐 Creating a Strapi project

按照以下步骤创建一个新的 Strapi 项目，确保使用适合你已安装的包管理器的命令：

🌐 Follow the steps below to create a new Strapi project, being sure to use the appropriate command for your installed package manager:

1. 在终端中，运行以下命令：

    <Tabs groupId="yarn-npm">

    <TabItem value="npm" label="NPM">

    ```bash
    npx create-strapi@latest
    ```

    <details>
    <summary>该命令的附加说明：</summary>

    * `npx` 从一个 npm 包运行命令
    * `create-strapi` 是 Strapi 包
    * `@latest` 表示使用的是 Strapi 的最新版本

    :::tip

    较旧的 `npx create-strapi-app@latest` 命令仍然有效，并且将提供与较新的 `npx create-strapi@latest` 命令完全相同的体验。

    :::

    除了 npx，也可以使用传统的 npm 命令，使用 `npm create strapi@latest`。

    请注意使用 npx 时在 create 和 strapi 之间的额外连字符：`npx create-strapi` 与 `npm create strapi`。
    </details>

    :::note

    在 Strapi Cloud 上，pnpm 版本由 [Corepack](https://github.com/nodejs/corepack#readme) 管理：项目中 `package.json` `packageManager` 字段中固定的版本会自动被使用，如果没有固定版本，则使用 Corepack 附带的默认版本。详情请参见 [包管理器版本](/cloud/projects/settings#modifying-node-version)。

    :::

    ```bash
    pnpm create strapi
    ```

2. 终端会询问你是否要在此步骤中选择 `Login/Signup` 或 `Skip`。使用箭头键并按下 `Enter` 来做出选择。如果你选择登录，你将获得一个 30 天的    计划试用，该计划将自动应用到你创建的项目中。如果你跳过此步骤，项目将回退到 CMS 免费计划。
3. 终端会问你几个问题。对于每个问题，如果你按 `Enter` 而不是输入内容，将使用默认答案（是）:

  ![安装时的终端提示](/img/assets/installation/prompts.png)

  :::tip

  你可以使用传递给安装命令的各种选项来跳过这些问题。请参考[表格](#cli-installation-options)获取所有可用选项的完整列表。

  :::

4. _(可选)_ 如果你在默认（SQLite）数据库问题上回答“否”为 `n`，CLI 会询问更多关于数据库的问题：
    * 使用箭头键选择你想要的数据库类型，然后按 `Enter`。
    * 给数据库命名，定义数据库主机地址和端口，定义数据库管理员用户名和密码，并定义数据库是否使用 SSL 连接。<br/>对于这些问题中的任何一个，如果你在没有输入任何内容的情况下按下 `Enter`，将使用默认值（在终端输出中以括号表示）。

回答完所有问题后，脚本将开始创建 Strapi 项目。

🌐 Once all questions have been answered, the script will start creating the Strapi project.

### CLI 安装选项 {#cli-installation-options}

🌐 CLI installation options

上述安装指南仅涵盖使用 CLI 的基本安装选项。在创建新的 Strapi 项目时，还可以使用其他选项，例如：

🌐 The above installation guide only covers the basic installation option using the CLI. There are other options that can be used when creating a new Strapi project, for example:

| 选项 | 描述 |
|-------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `--no-run` | 创建后不要启动应用 |
| `--ts`<br/>`--typescript` | 使用 TypeScript（默认）初始化项目 |
| `--js`<br/>`--javascript` | 使用 JavaScript 初始化项目 |
| `--use-npm` | 强制使用 [npm](https://www.npmjs.com/) 作为项目包管理器 |
| `--use-pnpm` | 强制使用 [pnpm](https://pnpm.nodejs.cn/) 作为项目包管理器 |
| `--install` | 安装所有依赖，跳过相关的命令行提示 |
| `--no-install` | 不安装所有依赖，跳过相关的 CLI 提示 |
| `--git-init` | 初始化一个 git 仓库，跳过相关的命令行提示 |
| `--no-git-init` | 不要初始化 git 仓库，跳过相关 CLI 提示 |
| `--example` | 添加示例数据，跳过相关的命令行提示 |
| `--no-example` | 不要添加示例数据，跳过相关的 CLI 提示 |
| `--skip-cloud` | 跳过 [Strapi 登录和项目创建步骤](#skipping-the-strapi-login-step) |
| `--non-interactive` | 跳过所有交互提示并使用默认设置（TypeScript、安装依赖、初始化 git、SQLite 数据库、无 A/B 测试）。使用此标志时需要提供 `<directory>` 参数。 |
| `--enable-ab-tests`<br/>`--no-enable-ab-tests` | 启用或禁用匿名 A/B 测试，跳过相关的命令行提示 |
| `--skip-db` | 跳过所有与数据库相关的提示，并创建一个使用默认（SQLite）数据库的项目 |
| `--template <template-name-or-url>` | 根据给定模板创建应用。<br/>模板的其他选项可用，详情请参见[模板文档](/cms/templates)。 |
| `--dbclient <dbclient>` | 通过在命令中将 `<dbclient>` 替换为以下值之一来定义要使用的数据库客户端：<ul><li>`sql` 用于 SQLite 数据库（默认）</li><li>`postgres` 用于 PostgreSQL 数据库</li><li>`mysql` 用于 MySQL 数据库</li></ul> |
| `--dbhost <dbhost>` | 通过将命令中的 `<dbhost>` 替换为你选择的值来定义要使用的数据库主机 |
| `--dbport <dbport>` | 通过将命令中的 `<dbport>` 替换为你选择的值来定义要使用的数据库端口 |
| `--dbname <dbname>` | 通过将命令中的 `<dbname>` 替换为你选择的值来定义要使用的数据库名称 |
| `--dbusername <dbusername>` | 通过将命令中的 `<dbusername>` 替换为你选择的值来定义要使用的数据库用户名 |
| `--dbpassword <dbpassword>` | 通过将命令中的 `<dbpassword>` 替换为你选择的值来定义要使用的数据库密码 |
| `--dbssl <dbssl>` | 通过传递 `--dbssl=true`（默认不使用 SSL）来定义数据库使用 SSL |
| `--dbfile <dbfile>` | 对于 SQLite 数据库，通过将命令中的 `<dbclient>` 替换为你选择的值来定义要使用的数据库文件路径 |
| `--quickstart` | (**在 Strapi 5 中已弃用**)<br/>直接以快速启动模式创建项目。请改用 `--non-interactive`。 |

:::note Notes

* 如果你没有传递 `--use-npm|pnpm` 选项，安装脚本将使用创建命令时使用的包管理器来安装所有依赖（例如，`npm create strapi` 将使用 npm 安装项目的所有依赖）。
* 有关数据库配置的更多信息，请参阅[数据库配置文档](/cms/configurations/database)。
* 实验性的 Strapi 版本每周二至周六格林威治标准时间午夜发布。你可以使用 `npx create-strapi@experimental` 基于最新的实验性发布创建一个新的 Strapi 应用。请自行承担使用这些实验性版本的风险。不建议在生产环境中使用它们。

:::

### 跳过 Strapi 登录步骤 {#skipping-the-strapi-login-step}

🌐 Skipping the Strapi login step

当安装脚本运行时，终端会首先询问你是否要登录/注册。选择 `Login/signup` 将为你提供    的 30 天试用，该试用将自动应用到你创建的项目中。这将使你可以访问高级 CMS 功能。

如果你想跳过这个 Strapi 登录部分，请使用箭头键选择 `Skip`。脚本将继续并使用 CMS 免费计划创建本地项目。

🌐 If you prefer skipping this Strapi login part, use the arrow keys to select `Skip`. The script will resume and create a local project using the CMS Free plan.

你稍后可以通过结账我们的 [pricing page](https://strapi.io/pricing-self-hosted)来购买CMS许可证。

### 托管你的项目 {#hosting-your-project}

🌐 Hosting your project

你可以将项目部署到 [Strapi Cloud](/cloud/intro)。要在线托管，你可以选择：

🌐 You can deploy a project to [Strapi Cloud](/cloud/intro). To host it online, you can choose to:

- 在遵循[部署指南](/cms/deployment)之前，通过将项目的代码推送到仓库（例如 GitHub）来自行托管它，
- 或者使用 [Cloud CLI](/cloud/cli/cloud-cli) 命令登录 Strapi Cloud，将本地项目链接到现有的 Strapi Cloud 项目，并免费在那里部署。

如果你想自己托管项目并且还不熟悉 GitHub，以下可切换的内容应该可以帮助你入门👇。

🌐 If you want to host your project yourself and are not already familiar with GitHub, the following togglable content should get you started👇.

<details>
<summary>将你的 Strapi 项目代码推送到 GitHub 所需的步骤：</summary>

1. 在终端中，确保你位于托管你创建的 Strapi 项目的文件夹中。
2. 运行 `git init` 命令来初始化此文件夹的 git。
3. 运行 `git add .` 命令以将所有已修改的文件添加到 git 索引中。
4. 运行 `git commit -m "Initial commit"` 命令以创建包含所有新增更改的提交。
5. 登录到你的 GitHub 账户并 [create a new repository](https://docs.github.com/en/repositories/creating-and-managing-repositories/quickstart-for-repositories)。给新的仓库起一个名字，例如 `my-strapi-project`，并记住这个名字。
6. 返回终端并将本地存储库推送到 GitHub：

  a. 运行类似以下的命令：`git remote add origin git@github.com:yourname/my-strapi-project.git`，确保将 `yourname` 替换为你自己的 GitHub 个人资料名称，将 `my-strapi-project` 替换为你在第5步中使用的实际名称。

  b. 运行 `git push --set-upstream origin main` 命令，将提交最终推送到你的 GitHub 仓库。

关于使用命令行接口进行 git 操作的更多信息可以在 [official GitHub documentation](https://docs.github.com/en/migrations/importing-source-code/using-the-command-line-to-import-source-code/adding-locally-hosted-code-to-github#adding-a-local-repository-to-github-using-git)中找到。

<br/>

</details>

## 运行 Strapi {#running-strapi}

🌐 Running Strapi

要启动 Strapi 应用，请在项目文件夹中运行以下命令：

🌐 To start the Strapi application, run the following command in the project folder:

```bash
npm run develop
```

:::info Where is my content?

对于自托管的 Strapi 项目，所有内容都会保存在项目文件夹下 `.tmp` 子文件夹中的数据库文件中（默认使用 SQLite）。因此，每当你从创建 Strapi 项目的文件夹启动 Strapi 应用时，你的内容都会可用（有关更多信息，请参阅 [数据库配置](/cms/configurations/database)）。

🌐 For self-hosted Strapi projects, all your content is saved in a database file (by default, SQLite) found in the `.tmp` subfolder in your project's folder. So anytime you start the Strapi application from the folder where you created your Strapi project, your content will be available (see [database configuration](/cms/configurations/database) for additional information).

如果内容被添加到 Strapi Cloud 项目中，它将存储在由你的 Strapi Cloud 项目管理的数据库中（有关更多信息，请参见 [Strapi Cloud 的高级数据库配置](/cloud/advanced/database)）。

🌐 If the content was added to a Strapi Cloud project, it is stored in the database managed with your Strapi Cloud project (see [advanced database configuration for Strapi Cloud](/cloud/advanced/database) for additional information).

:::
