Skip to content

Javascript

This module executes custom JavaScript code with full access to the workflow context. The script must be an asynchronous function that receives data (input data) and ctx (context). The context includes access to the workflow’s persistent memory (ctx.memory), logger (ctx.logger), moment.js (ctx.moment), lodash (ctx._), and JSON parsing utilities. The script runs in a vm sandbox with a 10-second timeout. It is the most flexible module for custom business logic, allowing reading and writing to the workflow memory, manipulating data with lodash, and performing asynchronous operations.

ParameterTypeRequiredDescription
scriptcodeYesAsynchronous JavaScript function. Format: async function(data, ctx) { ... return data; }. Has access to ctx.memory, ctx.logger, ctx.moment, ctx._ (lodash).
{
"nextModule": "siguiente_modulo",
"data": {
"count": 3,
"timestamp": "2024-01-15T10:30:00+01:00",
"processed": true
}
}
{
"script": "async function(data, ctx) {\n const count = await ctx.memory.load('counter') || 0;\n const now = ctx.moment().format();\n await ctx.memory.save('counter', count + 1);\n ctx.logger.info(`Iteracion ${count + 1} a las ${now}`);\n return { count: count + 1, timestamp: now };\n}"
}
  • The script must be an asynchronous function: async function(data, ctx) { ... }
  • Available context (ctx):
    • ctx.memory.load(key) — load value from workflow memory
    • ctx.memory.save(key, value, persistent) — save value to memory
    • ctx.memory.update(key, value) — update existing value
    • ctx.memory.delete(key) — delete value from memory
    • ctx.logger — workflow logger
    • ctx.moment — moment.js library for dates
    • ctx._ — lodash for data manipulation
    • ctx.parseMarkdownJsonString — utility for parsing JSON from markdown
  • Execution timeout is 10 seconds
  • If the script returns an object with a data property, that property is used as output
  • Input _meta_ metadata is preserved and merged with the script’s metadata
  • For simple expressions without context access, use evaluateExpression
  • evaluateExpression (simple expressions in vm2 sandbox)
  • phpRunner (execute PHP code)
  • pythonRunner (execute Python code)
  • mapFunction (apply function to each element of an array)