Tips > Building Workflows

Debug Workflows by Logging Intermediate Data

When a workflow produces unexpected output and the execution log is not detailed enough, use a Code node to write intermediate data to a JSON file on disk.

To debug an n8n workflow in production, add a Code node that writes each item's data to a timestamped JSON file on disk and then passes the items through unchanged. You capture the exact intermediate data at that point without the manual execution UI, so you can see which node corrupted the output. Remove the debug node before deploying.

When should you log intermediate data to a file?

When a workflow produces unexpected output and the execution log is not detailed enough, use a Code node to write intermediate data to a JSON file on disk. This is invaluable for debugging production issues where you cannot use the manual execution UI.

Real-world example: A data pipeline transforms records through 8 nodes and the final output is wrong, but you cannot tell which node introduced the problem.

How do you write intermediate data to disk from a Code node?

// Mode: Run Once for All Items
const fs = require('fs');
const path = require('path');

const debugDir = '/tmp/n8n-debug';
if (!fs.existsSync(debugDir)) {
  fs.mkdirSync(debugDir, { recursive: true });
}

const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filename = path.join(
  debugDir,
  `debug_${$workflow.name.replace(/\s+/g, '_')}_${timestamp}.json`
);

const debugPayload = {
  executionId: $execution.id,
  workflowName: $workflow.name,
  timestamp: new Date().toISOString(),
  itemCount: $input.all().length,
  items: $input.all().map((item, idx) => ({
    index: idx,
    jsonKeys: Object.keys(item.json),
    json: item.json,
    hasBinary: !!item.binary && Object.keys(item.binary).length > 0,
    binaryKeys: item.binary ? Object.keys(item.binary) : [],
  })),
};

fs.writeFileSync(filename, JSON.stringify(debugPayload, null, 2));

// Pass items through unchanged so the workflow is not affected
const items = $input.all();

// Add debug metadata to first item
items[0].json._debugFile = filename;

return items;

Danger: Remove Before Production. Always remove or disable debug Code nodes before deploying to production. Writing to disk on every execution will consume storage and potentially expose sensitive data. Use n8n's built-in Execution Data features for long-term monitoring instead.

Which debugging method should you use?

Debug Method When to Use
Debug Code node (this tip) Complex data issues in production pipelines
Manual execution with pin data Developing and testing individual nodes
console.log in Code node Quick checks visible in Docker/server logs
Execution history inspection Reviewing past runs after the fact

Related: Use Edit Fields in "Map Each" Mode for Simple Renames · Configure Payload Size and Binary Data Mode for Large Files

Showcase builds

19 complete workflows from my own projects, each with its n8n workflow JSON to import. Showcase entries link the file at the end of the article.

See the showcase builds

Keep reading

190 entries grouped by topic, from first workflow to queue mode. Free, no signup.

Browse the encyclopedia

Need it built?

I design, build and run n8n systems for clients. Every engagement starts with a $1,500 diagnostic audit, credited toward the build.

Book an introductory call