Skip to content

n8n 的 CLI 命令#

¥CLI commands for n8n

n8n 包含一个 CLI(命令行接口),允许你使用 CLI 而不是 n8n 编辑器来执行操作。这些指南包括启动工作流,以及导出和导入工作流和凭据。

¥n8n includes a CLI (command line interface), allowing you to perform actions using the CLI rather than the n8n editor. These include starting workflows, and exporting and importing workflows and credentials.

运行 CLI 命令#

¥Running CLI commands

你可以将 CLI 命令与自托管的 n8n 一起使用。根据你安装 n8n 的方式,运行命令的方式会有所不同:

¥You can use CLI commands with self-hosted n8n. Depending on how you choose to install n8n, there are differences in how to run the commands:

  • npm:n8n 命令可直接使用。以下示例文档中使用此格式。

¥npm: the n8n command is directly available. The documentation uses this in the examples below.

  • Docker:n8n 命令可在你的 Docker 容器内使用:

¥Docker: the n8n command is available within your Docker container:

1
docker exec -u node -it <n8n-container-name> <n8n-cli-command>

启动工作流#

¥Start a workflow

你可以使用 CLI 直接启动工作流。

¥You can start workflows directly using the CLI.

按 ID 执行已保存的工作流:

¥Execute a saved workflow by its ID:

1
n8n execute --id <ID>

更改工作流的活动状态#

¥Change the active status of a workflow

你可以使用 CLI 更改工作流的活动状态。

¥You can change the active status of a workflow using the CLI.

Restart required

这些命令操作你的 n8n 数据库。如果在 n8n 运行时执行这些操作,则更改不会生效,直到你重启 n8n 为止。

¥These commands operate on your n8n database. If you execute them while n8n is running, the changes don't take effect until you restart n8n.

将工作流 ID 的活动状态设置为 false:

¥Set the active status of a workflow by its ID to false:

1
n8n update:workflow --id=<ID> --active=false

将工作流 ID 的活动状态设置为 true:

¥Set the active status of a workflow by its ID to true:

1
n8n update:workflow --id=<ID> --active=true

将所有工作流的活动状态设置为 false:

¥Set the active status to false for all the workflows:

1
n8n update:workflow --all --active=false

将所有工作流的活动状态设置为 true:

¥Set the active status to true for all the workflows:

1
n8n update:workflow --all --active=true

导出实体#

¥Export entities

你可以使用命令行接口 (CLI) 从 n8n 导出数据库实体。此工具允许你从一个数据库类型(例如 SQLite)导出所有实体类型,并将其导入到另一个数据库类型(例如 Postgres)。

¥You can export your database entities from n8n using the CLI. This tooling allows you to export all entity types from one database type, such as SQLite, and import them into another database type, such as Postgres.

命令标志:

¥Command flags:

标记 描述
--help 帮助提示。
--outputDir 输出目录路径
--includeExecutionHistoryDataTables 包含执行历史数据表,默认情况下这些表会被排除,因为它们可能非常大。
1
n8n export:entities --outputDir=./outputs --includeExecutionHistoryDataTables=true

导出工作流和凭据#

¥Export workflows and credentials

你可以使用 CLI 从 n8n 导出工作流和凭据。

¥You can export your workflows and credentials from n8n using the CLI.

命令标志:

¥Command flags:

标记 描述
--help 帮助提示。
--all 导出所有工作流/凭据。
--backup 为备份设置 --all --pretty --separate 参数。你可以选择设置 --output。
--id 要导出的工作流 ID。
--output 如果使用单独的文件,则输出文件名或目录。
--pretty 以更易读的方式格式化输出。
--separate 每个工作流导出一个文件(便于版本控制)。必须使用 --output 设置目录。
--decrypted 以纯文本格式导出凭据。

工作流#

¥Workflows

将所有工作流导出到标准输出(终端):

¥Export all your workflows to the standard output (terminal):

1
n8n export:workflow --all

按 ID 导出工作流并指定输出文件名:

¥Export a workflow by its ID and specify the output file name:

1
n8n export:workflow --id=<ID> --output=file.json

将所有工作流导出到指定目录的单个文件中:

¥Export all workflows to a specific directory in a single file:

1
n8n export:workflow --all --output=backups/latest/file.json

使用 --backup 标志(详情见上文)将所有工作流导出到指定目录的单个文件中:

¥Export all the workflows to a specific directory using the --backup flag (details above):

1
n8n export:workflow --backup --output=backups/latest/

凭据#

¥Credentials

将所有凭据导出到标准输出(终端):

¥Export all your credentials to the standard output (terminal):

1
n8n export:credentials --all

按 ID 导出凭据并指定输出文件名:

¥Export credentials by their ID and specify the output file name:

1
n8n export:credentials --id=<ID> --output=file.json

将所有凭据导出到指定目录的单个文件中:

¥Export all credentials to a specific directory in a single file:

1
n8n export:credentials --all --output=backups/latest/file.json

使用 --backup 标志(详情见上文)将所有凭据导出到指定目录:

¥Export all the credentials to a specific directory using the --backup flag (details above):

1
n8n export:credentials --backup --output=backups/latest/

以纯文本格式导出所有凭据。你可以使用此方法将安装从一个版本迁移到另一个版本,即使配置文件中的密钥不同。

¥Export all the credentials in plain text format. You can use this to migrate from one installation to another that has a different secret key in the configuration file.

Sensitive information

所有敏感信息在文件中均可见。

¥All sensitive information is visible in the files.

1
n8n export:credentials --all --decrypted --output=backups/decrypted.json

导入实体#

¥Import entities

你可以使用此命令从之前的 export:entities 命令导入实体,它允许将实体导入到与导出数据库类型不同的数据库类型中。当前支持的数据库类型包括:SQLite,Postgres

¥You can import entities from a previous export:entities command using this command, it allows importing of entities into a database type that differs from the exported database type. Current supported database types include: SQLite, Postgres.

导入前数据库应为空,可通过 --truncateTables 参数强制执行此操作。

¥The database is expected to be empty prior to import, this can be forced with the --truncateTables parameter.

命令标志:

¥Command flags:

标记 描述
--help 帮助提示。
--inputDir 用于存放要导入的输出文件的输入目录。
--truncateTables 导入前截断表格
1
n8n import:entities --inputDir ./outputs --truncateTables true

导入工作流和凭证#

¥Import workflows and credentials

你可以使用 CLI 从 n8n 导入工作流和凭据。

¥You can import your workflows and credentials from n8n using the CLI.

Update the IDs

导出工作流和凭据时,n8n 还会导出它们的 ID。如果你现有数据库中存在具有相同 ID 的工作流和凭据,它们将被覆盖。为避免此错误,请在导入之前删除或更改 ID。

¥When exporting workflows and credentials, n8n also exports their IDs. If you have workflows and credentials with the same IDs in your existing database, they will be overwritten. To avoid this, delete or change the IDs before importing.

可用的标志:

¥Available flags:

标记 描述
--help 帮助提示。
--input 如果使用 --separate 参数,则输入文件名或目录。
--projectId 将工作流或凭据导入到指定项目。不能与 --userId 同时使用。
--separate --input 提供的目录导入 *.json 文件。
--userId 将工作流或凭据导入到指定用户。不能与 --projectId 同时使用。

Migrating to SQLite

n8n 将工作流和凭证名称限制为 128 个字符,但 SQLite 不强制执行长度限制。

¥n8n limits workflow and credential names to 128 characters, but SQLite doesn't enforce size limits.

导入过程中可能会出现“数据长度超过列名限制”之类的错误。

¥This might result in errors like Data too long for column name during the import process.

在这种情况下,你可以从 n8n 界面编辑名称并重新导出,或者在导入前直接编辑 JSON 文件。

¥In this case, you can edit the names from the n8n interface and export again, or edit the JSON file directly before importing.

工作流#

¥Workflows

从指定文件导入工作流:

¥Import workflows from a specific file:

1
n8n import:workflow --input=file.json

从指定目录导入所有工作流文件(JSON 格式):

¥Import all the workflow files as JSON from the specified directory:

1
n8n import:workflow --separate --input=backups/latest/

凭据#

¥Credentials

从指定文件导入凭据:

¥Import credentials from a specific file:

1
n8n import:credentials --input=file.json

从指定目录导入所有凭据文件(JSON 格式):

¥Import all the credentials files as JSON from the specified directory:

1
n8n import:credentials --separate --input=backups/latest/

许可证#

¥License

清除#

¥Clear

从 n8n 数据库中清除你现有的许可证,并将 n8n 重置为默认功能:

¥Clear your existing license from n8n's database and reset n8n to default features:

1
n8n license:clear

如果你的许可证包含 浮动权限,运行此命令还会尝试将其释放回池中,使其可供其他实例使用。

¥If your license includes floating entitlements, running this command will also attempt to release them back to the pool, making them available for other instances.

信息#

¥Info

显示有关现有许可证的信息:

¥Display information about the existing license:

1
n8n license:info

用户管理#

¥User management

你可以使用 n8n CLI 重置用户管理。此操作会将用户管理恢复到设置前的状态。它会删除所有用户账户。

¥You can reset user management using the n8n CLI. This returns user management to its pre-setup state. It removes all user accounts.

如果你忘记了密码,并且没有设置 SMTP 以通过电子邮件重置密码,请使用此选项。

¥Use this if you forget your password, and don't have SMTP set up to do password resets by email.

1
n8n user-management:reset

为用户禁用多因素身份验证 (MFA)#

¥Disable MFA for a user

如果用户丢失了恢复代码,你可以使用此命令禁用该用户的 MFA。用户随后可以重新登录并再次设置 MFA。

¥If a user loses their recovery codes you can disable MFA for a user with this command. The user will then be able to log back in to set up MFA again.

1
n8n mfa:disable --email=johndoe@example.com

禁用 LDAP#

¥Disable LDAP

你可以使用以下命令重置 LDAP 设置。

¥You can reset the LDAP settings using the command below.

1
n8n ldap:reset

卸载社区节点和凭证#

¥Uninstall community nodes and credentials

你可以使用 n8n CLI 管理 社区节点。目前,你只能卸载社区节点和凭据,这在社区节点导致系统不稳定时非常有用。

¥You can manage community nodes using the n8n CLI. For now, you can only uninstall community nodes and credentials, which is useful if a community node causes instability.

命令标志:

¥Command flags:

标记 描述
--help 显示 CLI 帮助。
--credential 凭证类型。通过访问节点的 <NODE>.credential.ts 文件并获取 name 的值来获取此值。
--package 社区节点的包名。
--uninstall 卸载节点。
--userId 拥有凭据的用户的 ID。在自托管实例上,查询数据库。在云端,请使用你的 API 密钥查询 API。

节点#

¥Nodes

按包名卸载社区节点:

¥Uninstall a community node by package name:

1
n8n community-node --uninstall --package <COMMUNITY_NODE_NAME>

例如,要卸载 Evolution API 社区节点,请输入:

¥For example, to uninstall the Evolution API community node, type:

1
n8n community-node --uninstall --package n8n-nodes-evolution-api

凭据#

¥Credentials

卸载社区节点凭证:

¥Uninstall a community node credential:

1
n8n community-node --uninstall --credential <CREDENTIAL_TYPE> --userId <ID>

例如,要卸载 Evolution API 社区节点凭据,请访问 repository 并导航至 credentials.ts file 以找到 name

¥For example, to uninstall the Evolution API community node credential, visit the repository and navigate to the credentials.ts file to find the name:

1
n8n community-node --uninstall --credential evolutionApi --userId 1234

安全审计#

¥Security audit

你可以对 n8n 实例运行 安全审计,以检测常见的安全问题。

¥You can run a security audit on your n8n instance, to detect common security issues.

1
n8n audit