Code Runner

Execute custom JavaScript code

Overview

This node, named "Code Runner," allows users to execute custom JavaScript code on the input data within an n8n workflow. It is designed for scenarios where built-in nodes do not provide the required transformation or processing logic, enabling advanced and flexible data manipulation.

Typical use cases include:

  • Custom filtering, mapping, or enriching of incoming data.
  • Implementing complex conditional logic that depends on multiple fields.
  • Combining or restructuring data items in ways not supported by standard nodes.

For example, a user might write JavaScript code to add a new field to each item based on existing data or to filter out items that do not meet certain criteria.

Properties

Name Meaning
JavaScript Code The custom JavaScript code to execute. This code receives the input items as items and should return an array of output items. The code editor supports multiline JavaScript editing.

Output

The node outputs an array of items, each with a json property containing the processed data. The structure of the output JSON depends entirely on the user's custom JavaScript code. Typically, each output item is an object with a json key holding the transformed data.

No binary data output is supported by default; the output is purely JSON-based.

Dependencies

  • No external services or API keys are required.
  • The node relies on the JavaScript runtime environment provided by n8n.
  • No additional configuration or environment variables are necessary.

Troubleshooting

  • Common issues:

    • Syntax errors or runtime exceptions in the custom JavaScript code will cause the node to fail.
    • Returning invalid data structures (anything other than an array of items) may lead to unexpected behavior.
  • Error messages:

    • If the JavaScript code throws an error, the node will report:
      Code execution failed: <error message>
  • Resolution tips:

    • Carefully check the JavaScript syntax and logic.
    • Ensure the code returns an array of objects formatted as expected by n8n ([{ json: {...} }, ...]).
    • Use try-catch blocks inside your code if you want to handle errors gracefully.
    • Test the code snippet independently before using it in the node.

Links and References

Discussion