Javascript
Description
Section titled “Description”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.
Configuration
Section titled “Configuration”| Parameter | Type | Required | Description |
|---|---|---|---|
| script | code | Yes | Asynchronous JavaScript function. Format: async function(data, ctx) { ... return data; }. Has access to ctx.memory, ctx.logger, ctx.moment, ctx._ (lodash). |
Output
Section titled “Output”{ "nextModule": "siguiente_modulo", "data": { "count": 3, "timestamp": "2024-01-15T10:30:00+01:00", "processed": true }}Usage Example
Section titled “Usage Example”Basic case
Section titled “Basic case”{ "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 memoryctx.memory.save(key, value, persistent)— save value to memoryctx.memory.update(key, value)— update existing valuectx.memory.delete(key)— delete value from memoryctx.logger— workflow loggerctx.moment— moment.js library for datesctx._— lodash for data manipulationctx.parseMarkdownJsonString— utility for parsing JSON from markdown
- Execution timeout is 10 seconds
- If the script returns an object with a
dataproperty, 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
Related Nodes
Section titled “Related Nodes”- evaluateExpression (simple expressions in vm2 sandbox)
- phpRunner (execute PHP code)
- pythonRunner (execute Python code)
- mapFunction (apply function to each element of an array)