Back to Nodes

Centroid

Last updated Feb 7, 2025

An n8n custom node to calculate the centroid of an array of vectors.

0 Weekly Downloads
9 Monthly Downloads

Included Nodes

Centroid

Description

n8n-nodes-rckflr-centroid

npm version
CI
License: MIT
Coverage Status

n8n community node that calculates the centroid of a list of numeric vectors. It is built for data pipelines that require averaging coordinates, embeddings or any multidimensional numeric representation.

Features

  • Accepts vectors provided directly in the node or via incoming items.
  • Validates dimensional consistency and numeric values before processing.
  • Supports batch processing: each incoming item is evaluated independently.
  • Optionally merges the calculated centroid with the original item payload.

Requirements

  • n8n >= 1.40.0
  • Node.js >= 20.0.0
  • pnpm >= 9.1.0

Installation

pnpm add n8n-nodes-rckflr-centroid

After installing, restart your n8n instance and add Centroid from the node palette.

Usage

  1. Choose the Vector Source:
    • Auto (Parameter or Item): Uses the parameter if provided; otherwise reads from the incoming item (array at item.json or from item.json.vectors).
    • Parameter: Read vectors from the Array of Vectors parameter (e.g. [[1,2,3],[4,5,6]]).
    • Item JSON (Root Array): Read vectors from the root of item.json when it contains an array of vectors.
    • Item JSON (Path): Read vectors from a dot-separated path under item.json (e.g. data.vectors), configurable via Vector Path.
  2. Enable or disable Merge Output With Input depending on whether you want to keep the original item data.
  3. Execute the workflow. The node outputs a centroid array under the centroid key.

Example Workflow

An example workflow is available at examples/centroid-basic.json. Import it into n8n to see the node in action.

{
  "nodes": [
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "vectors",
              "value": "[[0,0],[10,10],[5,5]]"
            }
          ]
        },
        "options": {}
      },
      "name": "Set Vectors",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [280, 300]
    },
    {
      "parameters": {},
      "name": "Centroid",
      "type": "n8n-nodes-rckflr-centroid.centroid",
      "typeVersion": 1,
      "position": [540, 300]
    }
  ]
}

Inputs & Outputs

Aspect Description
Input Optional array of vectors. Provide via node parameter, item.json root array, or a dot-separated path under item.json (e.g. data.vectors).
Output Each item contains a centroid array. When merging is enabled, the centroid is added to the original item JSON.
Errors The node throws clear messages for malformed JSON, non-numeric components, empty vectors or mismatched dimensions.

Error Handling

The node uses NodeOperationError for predictable failures. Errors include an item index (when available) to help you pinpoint problematic entries quickly.

Development

pnpm install
pnpm lint        # ESLint rules for nodes and tests
pnpm test        # Vitest unit tests with 80% coverage target
pnpm build       # TypeScript build + icon copy to dist/

To run the node locally alongside n8n, follow the official local development guide.

Quality

  • Linting powered by ESLint (eslint-n8n-nodes-base ruleset).
  • Formatting enforced through Prettier.
  • Unit tests executed with Vitest with coverage thresholds at 80% for lines, functions, branches and statements.
  • Continuous integration configured in .github/workflows/ci.yml.

Release Process

  1. Update CHANGELOG.md with pending changes.
  2. Bump the version in package.json following SemVer.
  3. Tag the release (git tag vX.Y.Z) and push tags.
  4. Publish to npm with pnpm publish --access public.

See REQUIREMENTS.md for the full list of project standards.

License

Released under the MIT License (c) Mauricio Perera.