Using VS Code for learning, testen and debugging

Hi there, after building a few workflows with n8n, I figured out that sooner or later you’re going to need the Function nodes, and that this also means you really have to wrap your head around your the whole array, json and javascript data manipulation functionality. I then started to play around with all kinds of sample code in Visual Studio Code. When I got a better understanding of the whole concept (I think ;-), I then got the idea of a simple template, where I could then copy in the data from a Node, as well as the JavaScript code from the Function node. This now gives me one screen where I have both input data, logic and result, plus all the added debugging facilities. Maybe, in a couple of months I’m going to figure out that there are better ways of doing things. But for now, this helps me to getter a better understanding of how different approaches work. Maybe this may be of help to you too.

This is the code for the .js file in VS Code:

let item = // paste JSON data from previous Node ... WATCH OUT Function items only once vs. FunctionItem item once per input item

{
    "inputs": [
        {
            "id": "18691908716",
            "type": "contact_to_call"
        },
        {
            "id": "18858006081",
            "type": "contact_to_call"
        }
    ]
}

function func() { // copy JavaScript Code from Function Node ...

    return {
        inputs: item.inputs.map(item => {
            return {
                id: item.id
            }
        })
    }

}

// display result ... needs "outputCapture": "std" in launch.json / alternative: add break-point at console.log to view result in VARIABLES

console.log(func())

As well as the code for the launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "pwa-node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${file}",
            //"console": "internalConsole",
            "outputCapture": "std",
            //"args": ["key=test", "key=test too", "some more text"]
        }
    ]
}
2 Likes

Well that looks handy for testing without opening n8n. Nice work :raised_hands: