How to run n8n in debug mode with VSCode?

I would like to understand the internals of n8n and maybe contribute later. But I could not figure out how to set up some breakpoints and run n8n in debug mode with vscode. Thanks in advanced.

This launch.json solved my issue.

{
"version": "0.2.0",
"configurations": [
  {
    "type": "node",
    "request": "launch",
    "name": "Debug n8n",
    "program": "${workspaceFolder}/packages/cli/bin/n8n",
    "cwd": "${workspaceFolder}",
    "outFiles": [
      "${workspaceFolder}/packages/**/dist/*.js"
    ],
    "protocol": "inspector"
  }
]

}

3 Likes

Welcome to the community @software_artisan!

Thanks a lot for sharing your solution with the community!

Hey,
The process is broken for me when launch this debugger. It seems like the execution command is not called or it is blocked by some process. Out of a debugger mode, everything works smoothly though.
The error caught in the front is as follows: Screenshot from 2020-06-19 09-15-35
Have anyone already faced such a behaviour? Does anyone have any idea of what is going on in this case?
Thanks.

Edit
It seems the debugger tries to attach the subprocesses to the same port as the master one

-The error: Workflow execution process did crash for an unknown reason!
means that the process which was running the workflow crashed. As it did crash n8n can not display any additional information. The main reason why a workflow process would crash is that it runs out of memory. So are you processing big amounts of data that this could be the reason?

It is happening even if I run the start node on an empty workflow

Ah yes you can only run n8n once with the same port. So either you start it “normally” or you start it in debugging. You can not start it “normally” and then again with debugging. They are two totally separate processes.

It is something related to the debugging process,
When I add execution arguments such as --inspect or --harmony to the subprocess in /packages/cli/src/WorkflowRunner.ts L176, the flow doesn’t break but the still the debugger doesn’t stop on the breakpoints inside the subprocess.

const subprocess = fork(pathJoin(__dirname, 'WorkflowRunnerProcess.js'), [], {execArgv: ['--inspect']});

or

const subprocess = fork(pathJoin(__dirname, 'WorkflowRunnerProcess.js'), [], {execArgv: ['--harmony']});

In this case it is probably easier to not start the workflow in a sub-process and instead run it in the main process by setting the environment variable:

export EXECUTIONS_PROCESS=main
2 Likes

Thanks a lot for the tip, adding it to the launch.json file made it work :slight_smile:

{
    // 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": "node",
				"request": "launch",
				"name": "Debug n8n",
				"runtimeExecutable": "/home/toky/.nvm/versions/node/v12.16.3/bin/node",
				"program": "${workspaceFolder}/packages/cli/bin/n8n",
				"cwd": "${workspaceFolder}",
				"outFiles": [
					"${workspaceFolder}/packages/**/dist/*.js"
				],
				"env": {
				"EXECUTIONS_PROCESS": "main",
				},
				"protocol": "inspector",
			}
    ]
} 
1 Like

Great to hear. Thanks for posting the updated version.

Have fun!

1 Like

Has anyone figured out how to debug with Chrome, as described here?

The project has several src directories, so I’m not quite sure what to provide for the webRoot value in the file launch.json.