跳转至

节点文件结构#

在节点结构中遵循最佳实践和标准使您的节点更易于维护。如果其他人需要使用该代码,这会很有帮助。

您节点的文件和目录结构取决于:

  • 您节点的复杂性。
  • 是否使用节点版本控制。
  • 您在 npm 包中包含多少个节点。

必需的文件和目录#

您的节点必须包括:

  • 项目根目录中的 package.json 文件。这是任何 npm 模块所必需的。
  • 一个 nodes 目录,包含您节点的代码:
    • 该目录必须包含基础文件,格式为 <node-name>.node.ts。例如,MyNode.node.ts
    • n8n 推荐包含一个 codex 文件,包含您节点的元数据。codex 文件名必须与节点基础文件名匹配。例如,给定一个名为 MyNode.node.ts 的节点基础文件,codex 名称为 MyNode.node.json
    • nodes 目录可以包含其他文件和子目录,包括版本目录,以及跨多个文件分割的节点代码以创建模块化结构。
  • 一个 credentials 目录,包含您的凭证代码。该代码位于单个凭证文件中。文件名格式为 <node-name>.credentials.ts。例如,MyNode.credentials.ts

Modular structure#

You can choose whether to place all your node's functionality in one file, or split it out into a base file and other modules, which the base file then imports. Unless your node is very simple, it's a best practice to split it out.

A basic pattern is to separate out operations. Refer to the HttpBin starter node for an example of this.

For more complex nodes, n8n recommends a directory structure. Refer to the Airtable node or Microsoft Outlook node as examples.

  • actions: a directory containing sub-directories that represent resources.
    • Each sub-directory should contain two types of files:
    • An index file with resource description (named either <resourceName>.resource.ts or index.ts)
    • Files for operations <operationName>.operation.ts. These files should have two exports: description of the operation and an execute function.
  • methods: an optional directory dynamic parameters' functions.
  • transport: a directory containing the communication implementation.

Versioning#

If your node has more than one version, and you're using full versioning, this makes the file structure more complex. You need a directory for each version, along with a base file that sets the default version. Refer to Node versioning for more information on working with versions, including types of versioning.

Decide how many nodes to include in a package#

There are two possible setups when building a node:

  • One node in one npm package.
  • More than one node in a single npm package.

n8n supports both approaches. If you include more than one node, each node should have its own directory in the nodes directory.

A best-practice example for programmatic nodes#

n8n's built-in Airtable node implements a modular structure and versioning, following recommended patterns.

此页面是否
💬 微信

🚀 与作者交流

关注公众号
n8n实战笔记公众号
n8n实战笔记
📚 教程 💡 案例 🔧 技巧
添加微信
添加作者微信
1对1 专业指导
⚡ 快答 🎯 定制 🚀 支持