Treat your n8n workflows like code
One of our automations runs an AI agent, and for months that agent's whole brain lived inside n8n: the prompts, the output rules, the validation, all of it written as JavaScript strings pasted into code nodes on the canvas. Every edit went straight to production. No tests, no history, no way to know what changed last Tuesday.
n8n earns its place. The visual canvas is the right tool for wiring services together, and we build on it constantly. But code nodes quietly collect real logic, and the canvas becomes the only copy of software your business depends on.
A workflow edited live on the canvas is a machine whose only blueprint is the machine itself. When it breaks, you are measuring the broken part to figure out what the working part looked like.
The pattern: file, test, sync, verify
We moved the logic out one piece at a time. The shape that works:
The repo file is the source of truth. Each piece of real logic, the text scrubber, the validators, the prompts, the output contracts, became a plain JavaScript file in git with its own tests. No framework, just node running assertion scripts.
A sync script does the deploy. Here is the wrinkle that surprises people: n8n code nodes run sandboxed and cannot load files from disk. You cannot just import your module. So the sync script splices the file's contents into the workflow JSON and pushes it through n8n's API. Deploying is an explicit, versioned act instead of a live edit.
A drift detector keeps both copies honest. It pulls the live workflow and proves, byte for byte, that the code in the node matches the file in git. If someone hot-fixes the canvas directly, the detector catches it before the two copies quietly diverge.
What it bought us
That agent's brain is now 5 extracted pieces backed by 74 tests. Before any deploy, the tests run, the synced result gets syntax-checked, and old and new node output get compared to prove the change does only what it claims. The canvas still runs the show. It just stopped being the only place the show exists.
None of this needed new tools. Git, node, and n8n's own API cover the whole loop.
The rule
Wiring can live on the canvas. Logic lives in a file, in git, with a test, and the canvas holds a deployed copy of it. If losing your n8n instance tomorrow would mean rebuilding business logic from memory, some of your code is living in the wrong place.
Move one piece this week. The first extraction is the slow one, and every piece after it rides the same rails.