节点文件结构#
在节点结构中遵循最佳实践和标准使您的节点更易于维护。如果其他人需要使用代码,这会很有帮助。
您节点的文件和目录结构取决于:
- 您节点的复杂性。
- 是否使用节点版本控制。
- 您在 npm 包中包含多少个节点。
n8n 建议使用 n8n-node 工具来创建预期的节点文件结构。您可以根据需要自定义生成的脚手架以满足更复杂的需求。
必需的文件和目录#
您的节点必须包括:
- 项目根目录下的
package.json文件。每个 npm 模块都需要这个。 - 一个
nodes目录,包含您节点的代码: - 一个
credentials目录,包含您的凭据代码。此代码位于单个凭据文件中。文件名格式为<node-name>.credentials.ts。例如,MyNode.credentials.ts。
模块化结构#
您可以选择将节点的所有功能放在一个文件中,或将其拆分为基础文件和其他模块,然后基础文件导入这些模块。除非您的节点非常简单,否则最佳实践是将其拆分。
基本模式是分离操作。有关此示例,请参考 GithubIssues 启动器节点。
对于更复杂的节点,n8n 建议使用目录结构。请参考 Airtable 节点或 Microsoft Outlook 节点作为示例。
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.tsorindex.ts) - Files for operations
<operationName>.operation.ts. These files should have two exports:descriptionof the operation and anexecutefunction.
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.