Skip to main content

命令行接口 (CLI)

¥Command Line Interface (CLI)

Strapi 配备了功能齐全的命令行接口 (CLI),可让你在几秒钟内构建和管理你的项目。CLI 可与 yarnnpm 包管理器配合使用。

¥Strapi comes with a full featured Command Line Interface (CLI) which lets you scaffold and manage your project in seconds. The CLI works with both the yarn and npm package managers.

提醒

诸如 strapi admin:create-user 之类的交互式命令不会显示带有 npm 的提示。预计 npm 包管理器的修复将于 2023 年 3 月完成。同时,考虑使用 yarn 包管理器。

¥Interactive commands such as strapi admin:create-user don't display prompts with npm. A fix for the npm package manager is anticipated by March 2023. In the meantime, consider using the yarn package manager.

注意

建议仅在本地安装 Strapi,这需要在以下所有 strapi 命令前添加用于项目设置的包管理器(例如 npm run strapi helpyarn strapi help)或专用节点包执行器(例如 npx strapi help)。

¥It is recommended to install Strapi locally only, which requires prefixing all of the following strapi commands with the package manager used for the project setup (e.g npm run strapi help or yarn strapi help) or a dedicated node package executor (e.g. npx strapi help).

要使用 npm 传递选项,请使用以下语法:npm run strapi <command> -- --<option>

¥To pass options with npm use the syntax: npm run strapi <command> -- --<option>.

要使用 yarn 传递选项,请使用以下语法:yarn strapi <command> --<option>

¥To pass options with yarn use the syntax: yarn strapi <command> --<option>

ℹ️ Strapi v4 CLI commands removed from Strapi 5:

Strapi v4 中的 strapi installstrapi uninstallstrapi newstrapi watch-admin 命令已在 Strapi 5 中删除:

¥The strapi install, strapi uninstall, strapi new, and strapi watch-admin commands from Strapi v4 have been removed in Strapi 5:

Strapi v4 命令Strapi 5 等效
strapi install [plugin]使用与插件相对应的 npx 命令(在 Marketplace 上找到,参见 用户指南
strapi new使用等效的 yarn 或 npx 命令创建一个新的 Strapi 项目(参见 CLI 安装指南
strapi watch-adminyarn developnpm run develop 始终以 "watch-admin" 模式启动 Strapi 服务器。要在 Strapi 5 中禁用此功能,请运行 yarn develop --no-watch-adminnpm run develop --no-watch-admin

strapi develop

别名:dev

¥Alias: dev

启动一个启用自动重新加载的 Strapi 应用。

¥Start a Strapi application with auto-reloading enabled.

Strapi 在运行时修改/创建文件,并且在创建新文件时需要重新启动。为了实现这一点,strapi develop 添加了一个文件监视程序,并在必要时重新启动应用。

¥Strapi modifies/creates files at runtime and needs to restart when new files are created. To achieve this, strapi develop adds a file watcher and restarts the application when necessary.

Strapi 还添加了中间件来支持管理面板的 HMR(热模块更换)。这允许你自定义管理面板,而无需重新启动应用或运行单独的服务器。

¥Strapi also adds middlewares to support HMR (Hot Module Replacement) for the administration panel. This allows you to customize the administration panel without having to restart the application or run a separate server.

yarn strapi develop
options: [--no-build |--no-watch-admin |--browser |--debug |--silent]
  • strapi evolve --open
    启动启用自动重新加载的应用并在运行管理面板的情况下打开默认浏览器。

    ¥strapi develop --open
    Starts your application with auto-reloading enabled & open your default browser with the administration panel running.

  • strapi evolve --no-watch-admin
    在对管理面板代码进行更改时防止服务器自动重新加载。

    ¥strapi develop --no-watch-admin
    Prevents the server from auto-reload when changes are made to the admin panel code.

  • [已弃用] strapi evolve --no-build
    在启用自动重新加载的情况下启动你的应用并跳过管理面板构建过程

    ¥[DEPRECATED] strapi develop --no-build
    Starts your application with the auto-reloading enabled and skip the administration panel build process

  • [已弃用] strapi evolve --watch-admin
    启动你的应用,启用自动重新加载和前端开发服务器。它允许你自定义管理面板。

    ¥[DEPRECATED] strapi develop --watch-admin
    Starts your application with the auto-reloading enabled and the front-end development server. It allows you to customize the administration panel.

  • [已弃用] strapi evolve --watch-admin --browser 'google chrome'
    在启用自动重新加载和前端开发服务器的情况下启动你的应用。它允许你自定义管理面板。私有字段和 s 不会在有效负载中发送。

    ¥[DEPRECATED] strapi develop --watch-admin --browser 'google chrome'
    Starts your application with the auto-reloading enabled and the front-end development server. It allows you to customize the administration panel. Provide a browser name to use instead of the default one, false means stop opening the browser.

警告

你永远不应该使用此命令在生产环境中运行 Strapi 应用。

¥You should never use this command to run a Strapi application in production.

strapi start

启动一个禁用自动重新加载的 Strapi 应用。

¥Start a Strapi application with auto-reloading disabled.

此命令用于运行 Strapi 应用,无需重新启动和文件写入,主要用于生产。某些功能(例如内容类型生成器)在 strapi start 模式下被禁用,因为它们需要重新启动应用。start 命令可以以 环境变量 开头,以自定义应用启动。

¥This command is to run a Strapi application without restarts and file writes, primarily for use in production. Certain features such as the Content-type Builder are disabled in the strapi start mode because they require application restarts. The start command can be prefaced with environment variables to customize the application start.

yarn strapi start

strapi build

构建你的管理面板。

¥Builds your admin panel.

yarn strapi build
选项类型描述
-d, --debug*启用带有详细日志的调试模式(默认值:false)
--minify*最小化输出(默认值:true)
--no-optimization*
--silent*不记录任何内容(默认值:false)
--sourcemaps*生成源映射(默认值:false)
--stats*以前发布的内容可以取消发布。

strapi login

登录 Strapi Cloud(参见 Cloud CLI 文档)。

¥Logs in to Strapi Cloud (see Cloud CLI documentation).

yarn strapi login

strapi logout

从 Strapi Cloud 注销(参见 Cloud CLI 文档)。

¥Logs out from Strapi Cloud (see Cloud CLI documentation).

yarn strapi logout

strapi deploy

部署到 Strapi Cloud(参见 Cloud CLI 文档)。

¥Deploys to Strapi Cloud (see Cloud CLI documentation).

yarn strapi deploy

strapi export

导出你的项目数据。默认设置创建一个 .tar 文件,使用 gzip 压缩并使用 aes-128-ecb 加密。

¥Exports your project data. The default settings create a .tar file, compressed using gzip and encrypted using aes-128-ecb.

yarn strapi export

导出的文件将使用 export_YYYYMMDDHHMMSS 格式以及当前日期和时间戳自动命名。或者,你可以使用 -f--file 标志指定文件名。下表提供了作为命令行标志的所有可用选项:

¥The exported file is automatically named using the format export_YYYYMMDDHHMMSS with the current date and timestamp. Alternately, you can specify the filename using the -f or --file flag. The following table provides all of the available options as command line flags:

选项类型描述
‑‑no‑encrypt*禁用文件加密并禁用 key 选项。
‑‑no‑compress*禁用文件压缩。
-k
--key
string将加密密钥作为 export 命令的一部分传递。
--key 选项不能与 --no-encrypt 结合使用。
-f
--file
string指定导出文件名。请勿包含文件扩展名。
--excludestring使用逗号分隔的数据类型排除数据。可用的类型有:contentfilesconfig
--onlystring仅包含这些数据。可用的类型有:contentfilesconfig
-h
--help
*显示 strapi export 命令的帮助。

示例

¥Examples

Examples of strapi export:
# export your data with the default options and the filename myData, which results in a file named myData.tar.gz.enc.
yarn strapi export -f myData

# export your data without encryption.
yarn strapi export --no-encrypt

strapi import

导入数据 进入你的项目。导入的数据必须来自另一个 Strapi 应用。你必须传递 --file 选项来指定导入操作的文件名和位置。

¥Imports data into your project. The imported data must originate from another Strapi application. You must pass the --file option to specify the filename and location for the import action.

yarn strapi import
选项类型描述
-k, --keystring在命令中提供加密密钥,而不是随后的提示。
-f--filestring要导入的数据的路径和文件名以及扩展名。
-h--help*显示 strapi import 帮助命令。

示例

¥Examples

Example of strapi import:
# import your data with the default parameters and pass an encryption key:
yarn strapi import -f your-filepath-and-filename --key my-key

strapi transfer

2 个 Strapi 实例之间的 传输数据。此命令主要用于本地实例和一个远程实例或 2 个远程实例之间的使用。transfer 命令需要一个传输令牌,该令牌是在目标实例管理面板中生成的。有关创建 Transfer 令牌的详细文档,请参阅 用户指南

¥Transfers data between 2 Strapi instances. This command is primarily intended for use between a local instance and a remote instance or 2 remote instances. The transfer command requires a Transfer token, which is generated in the destination instance Admin panel. See the User Guide for detailed documentation on creating Transfer tokens.

提醒

目标 Strapi 实例应使用 start 命令而不是 develop 命令运行。

¥The destination Strapi instance should be running with the start command and not the develop command.

选项描述
--to [destinationURL]目标 Strapi 实例上 /admin 端点的完整 URL
(例如 --to https://my-beautiful-strapi-website/admin
--to-token [transferToken]传输远程 Strapi 目标的令牌
--from [sourceURL]用于从中提取数据的远程 Strapi 实例的 /admin 端点的完整 URL(例如 --from https://my-beautiful-strapi-website/admin
‑‑from‑token从 Strapi 源实例传输令牌。
--force自动回答 "yes" 所有提示,包括潜在的破坏性请求,并以非交互方式运行。
--exclude使用逗号分隔的数据类型排除数据。可用的类型有:contentfilesconfig
--only仅包含这些数据。可用的类型有:contentfilesconfig
-h--help显示 strapi transfer 的命令。
提醒

--to--from 为必填项,但目前不允许两者都输入或都不输入。

¥Either --to or --from is required, but it's not currently allowed to enter both or neither.

示例

¥Example

yarn strapi transfer --to http://example.com/admin --to-token my-transfer-token

strapi report

打印出对调试有用且报告问题时所需的调试信息。

¥Prints out debug information useful for debugging and required when reporting an issue.

选项描述
-u--uuid包括项目 UUID
-d--dependencies包括项目依赖
--all记录所有数据

示例

¥Examples

# To include the project UUID and dependencies in the output:
yarn strapi report --uuid --dependencies

# To log everything, use the --all option:
yarn strapi report --all

strapi configuration:dump

别名:config:dump

¥Alias: config:dump

将配置转储到文件或标准输出以帮助你迁移到生产环境。

¥Dumps configurations to a file or stdout to help you migrate to production.

转储格式将是 JSON 数组。

¥The dump format will be a JSON array.

strapi configuration:dump
yarn strapi configuration:dump

Options:
-f, --file <file> Output file, default output is stdout
-p, --pretty Format the output JSON with indentation and line breaks (default: false)

示例

¥Examples

yarn strapi configuration:dump -f dump.json
yarn strapi config:dump --file dump.json
yarn strapi config:dump > dump.json

所有这些例子都是等价的。

¥All these examples are equivalent.

提醒

配置应用时,你经常输入第三方服务(例如身份验证提供者)的凭据。请注意,这些凭据也将转储到此命令的输出中。如有疑问,你应该避免将转储文件提交到版本控制系统中。你可以探索以下一些方法:

¥When configuring your application you often enter credentials for third party services (e.g authentication providers). Be aware that those credentials will also be dumped into the output of this command. In case of doubt, you should avoid committing the dump file into a versioning system. Here are some methods you can explore:

  • 将文件直接复制到所需的环境并在那里运行恢复命令。

    ¥Copy the file directly to the environment you want and run the restore command there.

  • 将文件放在安全位置,并在部署时使用正确的凭据下载它。

    ¥Put the file in a secure location and download it at deploy time with the right credentials.

  • 在提交之前对文件进行加密,并在运行恢复命令时对其进行解密。

    ¥Encrypt the file before committing and decrypt it when running the restore command.

strapi configuration:restore

别名:config:restore

¥Alias: config:restore

将配置转储恢复到你的应用中。

¥Restores a configuration dump into your application.

输入格式必须是 JSON 数组。

¥The input format must be a JSON array.

yarn strapi configuration:restore

Options:
-f, --file <file> Input file, default input is stdin
-s, --strategy <strategy> Strategy name, one of: "replace", "merge", "keep". Defaults to: "replace"

示例

¥Examples

yarn strapi configuration:restore -f dump.json
yarn strapi config:restore --file dump.json -s replace
yarn strapi config:restore < dump.json

所有这些例子都是等价的。

¥All these examples are equivalent.

策略

¥Strategies

运行恢复命令时,你可以选择三种不同的策略:

¥When running the restore command, you can choose from three different strategies:

  • 代替:将创建丢失的密钥并替换现有的密钥。

    ¥replace: Will create missing keys and replace existing ones.

  • 合并:将创建丢失的键并将现有键与其新值合并。

    ¥merge: Will create missing keys and merge existing keys with their new value.

  • 保持:将创建丢失的密钥并按原样保留现有密钥。

    ¥keep: Will create missing keys and keep existing keys as is.

strapi admin:create-user

别名 admin:create

¥Alias admin:create

创建管理员。管理员的名字、姓氏、电子邮件和密码可以是:

¥Creates an administrator. Administrator's first name, last name, email, and password can be:

  • 作为选项传递

    ¥passed as options

  • 或者如果你调用命令而不传递任何选项,则以交互方式设置。

    ¥or set interactively if you call the command without passing any option.

示例

¥Example

yarn strapi admin:create-user --firstname=Kai --lastname=Doe --email=chef@strapi.io --password=Gourmet1234

选项

¥Options

选项类型描述必需的
-f, --名字string管理员的名字是的
-l, --姓氏string管理员的姓氏
-e, --电子邮件string管理员的电子邮件是的
-p, --密码string管理员的新密码
-h,--帮助显示命令的帮助

strapi admin:reset-user-password

别名 admin:reset-password

¥Alias admin:reset-password

重置管理员用户的密码。你可以将电子邮件和新密码作为选项传递,或者如果调用命令而不传递选项,则可以交互设置它们。

¥Reset an admin user's password. You can pass the email and new password as options or set them interactively if you call the command without passing the options.

示例

¥Example

yarn strapi admin:reset-user-password --email=chef@strapi.io --password=Gourmet1234

选项

¥Options

选项类型描述
-e, --电子邮件string用户电子邮件
-p, --密码string用户的新密码
-h,--帮助显示命令的帮助

strapi generate

运行完全交互式 CLI 来生成 API、controllerscontent-typespluginspoliciesmiddlewaresservices 以及 migrations

¥Run a fully interactive CLI to generate APIs, controllers, content-types, plugins, policies, middlewares and services, and migrations.

yarn strapi generate

strapi templates:generate

从当前 Strapi 项目创建模板。

¥Create a template from the current Strapi project.

yarn strapi templates:generate <path>
  • Strapi templates:generate <path>
    <path> 生成 Strapi 模板

    ¥strapi templates:generate <path>
    Generates a Strapi template at <path>

    示例:strapi templates:generate ../strapi-template-name 会将所需的文件和文件夹复制到 ../strapi-template-name 内的 template 目录中

    ¥Example: strapi templates:generate ../strapi-template-name will copy the required files and folders to a template directory inside ../strapi-template-name

strapi ts:generate-types

为项目模式生成 TypeScript 类型。

¥Generate TypeScript typings for the project schemas.

yarn strapi ts:generate-types
  • strapi ts:generate-types --debug
    在启用调试模式的情况下生成类型,显示生成的模式的详细表格。

    ¥strapi ts:generate-types --debug
    Generate typings with the debug mode enabled, displaying a detailed table of the generated schemas.

  • Strapi ts:generate-types --silent 或 Strapi ts:generate-types -s
    在启用静默模式的情况下生成类型,完全删除终端中的所有日志。不能与 debug 结合使用

    ¥strapi ts:generate-types --silent or strapi ts:generate-types -s
    Generate typings with the silent mode enabled, completely removing all the logs in the terminal. Cannot be combined with debug

  • Strapi ts:generate-types --out-dir <path> 或 Strapi ts:generate-types -o <path- --GT-BASE---
    生成指定将在其中创建文件的输出目录的类型。

    ¥strapi ts:generate-types --out-dir <path> or strapi ts:generate-types -o <path>
    Generate typings specifying the output directory in which the file will be created.

提醒

Strapi 要求在 types 目录中生成项目类型才能正常工作。大多数情况下不应使用 --out-dir 选项。但是,对于更改内容结构后生成第二个副本以比较现有类型和更新类型之间的差异等情况可能很有用。

¥Strapi requires the project types to be generated in the types directory for them to work. The --out-dir option should not be used for most cases. However, it can be useful for cases such as generating a second copy to compare the difference between your existing and updated types after changing your content structure.

strapi routes:lis

显示所有可用 routes 的列表。

¥Display a list of all the available routes.

yarn strapi routes:list

strapi policies:list

显示所有已注册的 policies 的列表。

¥Display a list of all the registered policies.

yarn strapi policies:list

strapi middlewares:list

显示所有已注册的 middlewares 的列表。

¥Display a list of all the registered middlewares.

yarn strapi middlewares:list

strapi content-types:list

显示所有现有 content-types 的列表。

¥Display a list of all the existing content-types.

yarn strapi content-types:list

strapi hooks:list

显示所有可用钩子的列表。

¥Display a list of all the available hooks.

yarn strapi hooks:list

strapi controllers:list

显示所有已注册的 controllers 的列表。

¥Display a list of all the registered controllers.

yarn strapi controllers:list

strapi services:list

显示所有已注册的 services 的列表。

¥Display a list of all the registered services.

yarn strapi services:list

strapi telemetry:disable

禁用项目的数据收集(参见 使用信息)。

¥Disable data collection for the project (see Usage Information).

yarn strapi telemetry:disable

strapi telemetry:enable

禁用项目后重新启用数据收集(参见 使用信息)。

¥Re-enable data collection for the project after it was disabled (see Usage Information).

yarn strapi telemetry:enable

strapi console

实时启动应用中的服务器和 eval 命令。

¥Start the server and eval commands in your application in real time.

yarn strapi console

strapi version

将构建统计信息打印到控制台(默认值:false)如果此命令是全局安装的 strapi,它将输出当前全局安装的版本,或者如果从包含 Strapi 项目的给定文件夹运行该命令,它将输出 Strapi 项目中的当前 Strapi 版本。

¥Print the currently installed Strapi version. It will output the current globally installed version if this command is strapi is installed globally, or the current version of Strapi within a Strapi project if the command is run from a given folder containing a Strapi project.

yarn strapi version

strapi help

列出 CLI 命令。

¥List CLI commands.

yarn strapi help