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)#

您可以在自托管的 n8n 中使用 CLI 命令。根据您选择安装 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 命令可以直接使用。文档在下面的示例中使用了它。
  • Docker:n8n 命令可以在你的 Docker 容器中使用:

    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.

需要重新启动

这些命令会作用于你的 n8n 数据库。如果在 n8n 运行时执行,它们的更改在重启 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.

敏感信息

所有敏感信息都可以在文件中看到。

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.

更新ID

导出工作流和凭证时,n8n 也会导出它们的 ID。如果你在现有数据库中已有相同 ID 的工作流和凭证,它们将被覆盖。为避免这种情况,请在导入前删除或更改 ID。

可用的标志:

🌐 Available flags:

标志 描述
--help 帮助提示。
--input 输入文件名或如果使用 --separate,则为目录。
--projectId 将工作流或凭证导入到指定的项目中。不能与 --userId 一起使用。
--separate 从 --input 提供的目录导入 *.json 文件。
--userId 将工作流或凭证导入到指定用户中。不能与 --projectId 一起使用。

迁移到 SQLite

n8n 将工作流和凭证名称限制为 128 个字符,但 SQLite 并不强制大小限制。

这可能会在导入过程中导致诸如 列名称数据过长 的错误。

在这种情况下,您可以从 n8n 界面编辑名称后再次导出,或者在导入前直接编辑 JSON 文件。

工作流(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:

Flag Description
--help Show CLI help.
--credential The credential type. Get this value by visiting the node's <NODE>.credential.ts file and getting the value of name.
--package Package name of the community node.
--uninstall Uninstalls the node.
--userId The ID of the user who owns the credential. On self-hosted, query the database. On cloud, query the API with your API key.

节点(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 社区节点凭证,请访问 仓库 并导航到 credentials.ts 文件 查找 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