Skip to content

节点版本控制#

n8n 支持节点版本控制。您可以通过引入新版本对现有节点进行更改,而不会破坏现有行为。

请注意 n8n 如何决定加载哪个节点版本:

  • 如果用户使用版本 1 构建并保存工作流,n8n 将继续在该工作流中使用版本 1,即使您创建并发布节点的版本 2。
  • 当用户创建新工作流并浏览节点时,n8n 始终加载节点的最新版本。

版本控制类型受节点样式限制

如果您使用声明式样式构建节点,则无法使用完整版本控制。

轻量版本控制#

这适用于所有节点类型。

一个节点可以包含多个版本,允许小的版本增量而无需代码重复。要使用此功能:

  1. 将主 version 参数更改为数组,并添加您的版本号,包括现有版本。
  2. 然后,您可以在任何对象的 displayOptions 中使用 @version 访问版本参数(以控制 n8n 显示对象的版本)。您还可以使用 const nodeVersion = this.getNode().typeVersion; 从函数查询版本。

例如,假设您想要为声明式节点教程中的 NasaPics 节点添加版本控制,然后配置资源,使 n8n 仅在节点的版本 2 中显示它。在您的基础 NasaPics.node.ts 文件中:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
{
    displayName: 'NASA Pics',
    name: 'NasaPics',
    icon: 'file:nasapics.svg',
    // 列出可用版本
    version: [1,2,3],
    // 这里有更多基本参数
    properties: [
        // 添加仅在版本2中显示的资源
        {
            displayName: 'Resource name',
            // 更多资源参数
            displayOptions: {
                show: {
                    '@version': 2,
                },
            },
        },
    ],
}

Feature-based versioning#

Feature flags let you control parameter visibility and execution logic based on named features tied to node versions.

Defining features#

Add a features object to your node type description. Each feature uses @version conditions to specify which versions enable it:

1
2
3
4
5
6
7
8
9
{
    version: [2, 2.1, 2.2, 2.3, 2.4],
    features: {
        useNewApi: { '@version': [{ _cnd: { gte: 2.2 } }] },
        useLegacyAuth: { '@version': [{ _cnd: { lte: 2.1 } }] },
        useSpecialMode: { '@version': [2] },
    },
    // More basic parameters here
}

Available conditions: gte, lte, gt, lt. Pass a plain version number to match a specific version.

Using @feature in displayOptions#

Use @feature in displayOptions to control parameter visibility based on feature flags:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
    displayName: 'New API Field',
    name: 'newApiField',
    type: 'string',
    displayOptions: {
        show: {
            '@feature': ['useNewApi'],
        },
    },
}

To show a parameter when a feature is not enabled, use the condition syntax:

1
2
3
4
5
displayOptions: {
    show: {
        '@feature': [{ _cnd: { not: 'useNewApi' } }],
    },
}

You can combine @feature with other display conditions:

1
2
3
4
5
6
displayOptions: {
    show: {
        resource: ['myResource'],
        '@feature': [{ _cnd: { eq: 'useNewApi' } }],
    },
}

Checking features in code#

Use this.isNodeFeatureEnabled() in execution contexts (such as IExecuteFunctions or IWebhookFunctions):

1
2
3
4
5
if (this.isNodeFeatureEnabled('useNewApi')) {
    // Process with new API
} else {
    // Process with legacy API
}

Full versioning#

这不适用于声明式节点。

例如,请参阅 Mattermost 节点

完整版本控制摘要:

  • 基础节点文件应该扩展 NodeVersionedType 而不是 INodeType
  • 基础节点文件应该包含描述,包括 defaultVersion(通常是最新版本)、其他基本节点元数据(如名称)和版本列表。它不应该包含任何节点功能。
  • n8n 建议使用 v1v2 等作为版本文件夹名称。