n8n 节点代码检查器(n8n node linter)#
n8n 的节点代码检查器 @n8n/eslint-plugin-community-nodes 会静态分析(“lint”)社区包中 n8n 节点和凭证的源代码。该检查器会检测问题并自动修复它们,以帮助你遵循最佳实践。
🌐 n8n's node linter, @n8n/eslint-plugin-community-nodes, statically analyzes ("lints") the source code of n8n nodes and credentials in community packages. The linter detects issues and automatically fixes them to help you follow best practices.
@n8n/eslint-plugin-community-nodes 包含了一个针对节点文件(*.node.ts)、凭证文件(*.credentials.ts)以及社区包的 package.json 的 规则集合。
设置(Setup)#
如果使用 n8n 节点启动器:在启动器项目中运行 npm install 安装所有依赖。安装完成后,你就可以使用代码检查工具了。
🌐 If using the n8n node starter: Run npm install in the starter project to install all dependencies. Once the installation finishes, the linter is available to you.
如果使用 VS Code,请安装 ESLint VS Code 扩展。对于其他 IDE,请参考它们的 ESLint 集成。
🌐 If using VS Code, install the ESLint VS Code extension. For other IDEs, refer to their ESLint integrations.
不要编辑配置文件
eslint.config.mjs 包含由 @n8n/node-cli 提供的 ESLint 配置。不要编辑此文件。
用法(Usage)#
你可以在社区包或 n8n 主仓库中使用代码检查器。
🌐 You can use the linter in a community package or in the main n8n repository.
代码检查(Linting)#
在社区包中,安装依赖后以及发布包到 npm 之前,会自动运行 linter。在主 n8n 仓库中,每当你向拉取请求推送代码时,linter 会使用 GitHub Actions 自动运行。
🌐 In a community package, the linter runs automatically after installing dependencies and before publishing the package to npm. In the main n8n repository, the linter runs automatically using GitHub Actions whenever you push to your pull request.
在这两种情况下,VS Code 会在你处理项目时在后台进行代码检查。将鼠标悬停在检测到的问题上,可以查看完整的检查说明以及进一步信息的链接。
🌐 In both cases, VS Code lints in the background as you work on your project. Hover over a detected issue to see a full description of the linting and a link to further information.
你也可以手动运行代码检查器:
🌐 You can also run the linter manually:
- 运行
npm run lint来进行代码检查,并在控制台查看检测到的问题。 - 运行
npm run lint:fix来进行代码检查并自动修复问题。代码检查器会修复 标记为可自动修复的 规则违规情况。
这两个命令可以在你的社区包的根目录下运行,也可以在主仓库中的 /packages/nodes-base/ 目录下运行。
🌐 Both commands can run in the root directory of your community package, or in /packages/nodes-base/ in the main repository.
例外(Exceptions)#
除了修复规则违规之外,你还可以为其创建例外,这样代码检查器就不会标记它。
🌐 Instead of fixing a rule violation, you can also make an exception for it, so the linter doesn't flag it.
在 VS Code 中对 lint 创建例外:将鼠标悬停在问题上,然后点击 Quick fix(在 macOS 上为 cmd+.),并选择 为此行禁用 {rule}。只有在有充分理由的情况下才为某一行禁用规则。如果你认为 linter 错误地报告了一个问题,请在 n8n 仓库中报告 。
🌐 To make a lint exception from VS Code: hover over the issue and click on Quick fix (or cmd+. in macOS) and select Disable {rule} for this line. Only disable rules for a line where you have good reason to. If you think the linter is incorrectly reporting an issue, please report it in the n8n repository.
要为单个文件添加 lint 异常,请添加代码注释。有关更多指导,请参考 ESLint 文档。
🌐 To add a lint exception to a single file, add a code comment. Refer to the ESLint documentation for more guidance.