How to Fix n8n Function Node Syntax Errors

Step by Step Guide to Fix n8n Function Node Syntax Errors

 


Who this is for: Workflow developers who receive “Unexpected token” or “Missing ) after argument list” errors in n8n’s Function node and need a fast, reliable way to locate and fix them. We cover this in detail in the n8n Node Specific Errors Guide.


Quick Diagnosis

Problem: “Unexpected token” or “Missing ) after argument list” errors appear when a Function node runs.

Quick fix: Open the Function node → enable “Show line numbers” → locate the highlighted line → correct the JavaScript syntax (e.g., close brackets, add missing commas, use proper await/return).

One‑click test: Click “Execute Node” after fixing; the node should return a green checkmark and the expected output.


1. Why Function‑Node Syntax Errors Occur in n8n

If you encounter any n8n set node type mismatch resolve them before continuing with the setup.

Root Cause Typical Error Message Example
Missing/extra punctuation (; , ( ) { }) SyntaxError: Unexpected token ) return { foo: bar } (missing comma)
Wrong use of await/async SyntaxError: Unexpected identifier await $http.get(...) inside a non‑async function
Variable scope misuse ($json, $item, $node) ReferenceError: $json is not defined return $json.id in a node that only has $item
Reserved word collision (e.g., class, function) SyntaxError: Unexpected token ‘class’ const class = "test"
Incorrect string interpolation SyntaxError: Unexpected token ` return `User ${user.id}` (missing back‑ticks)

n8n’s Function node runs pure JavaScript (Node.js v18) in a sandbox. Any syntax that the Node runtime can’t parse aborts the node before the workflow continues.


2. Enable Precise Debugging Tools

If you encounter any n8n merge node type mismatch resolve them before continuing with the setup.

Turn on visual aids that point you directly to the offending line.

  • Show line numbers – Click the gear icon ► Editor Settings ► toggle “Show line numbers”.
  • Activate linting – In the same settings panel, enable “ESLint”. n8n underlines syntax problems in real time.
  • Use the “Execute Node” button – Runs only the Function node, returning the exact error stack and the line number.

EEFA Note: In production workflows, never leave console.log statements that output sensitive data. Use $node["Debug"].json only in a dedicated debugging sub‑workflow.


3. Step‑by‑Step Workflow to Identify & Fix the Error

Step Action Expected Result
1 Open the failing Function node. Code editor appears.
2 Click Execute Node (no input needed). Error panel shows line X and error message.
3 Jump to the highlighted line (line numbers now visible). Cursor lands on the offending token.
4 Apply the appropriate fix (see tables below). No red underline; linting passes.
5 Re‑run Execute Node. Green checkmark + JSON output.
6 Save the node and run the full workflow to confirm downstream nodes work. Workflow completes without errors.

4. Common Syntax Mistakes & Exact Fixes

If you encounter any n8n function node reference error resolve them before continuing.

4.1 Missing Comma or Bracket

Problem: Objects need commas between properties and matching braces.

// ❌ Bad – missing comma and trailing brace
return {
  id: item.id
  name: item.name,
// ✅ Fixed – commas added, brace closed
return {
  id: item.id,
  name: item.name,
};

4.2 Improper await Usage

Problem: await can only be used inside an async function.

// ❌ Bad – node is not async
await $http.get('https://api.example.com');
// ✅ Fixed – wrap code in an async IIFE
return (async () => {
  const response = await $http.get('https://api.example.com');
  return response.json;
})();

4.3 Wrong Variable Scope

Problem: $json is unavailable in a “Function Item” node; use $item instead.

// ❌ Bad – $json undefined here
return $json.id;
// ✅ Fixed – reference the current item
return $item.json.id;

4.4 Reserved Word Collision

Problem: JavaScript reserves words like class.

// ❌ Bad – syntax error
const class = "premium";
// ✅ Fixed – rename variable
const userClass = "premium";

4.5 Bad Template Literals

Problem: Use back‑ticks for interpolation, not double quotes.

// ❌ Bad – no interpolation
return "User ID: ${item.id}";
// ✅ Fixed – back‑ticks enable ${}
return `User ID: ${item.id}`;

5. Advanced Debugging Techniques

*When simple fixes aren’t enough, these patterns help you capture runtime issues.*

Technique How‑to When to Use
try / catch wrapper
try { /* your logic */ } catch (e) { return { error: e.message }; }
To capture runtime errors that aren’t syntax‑related.
$node[“<NodeName>”].json inspection
return $node["HTTP Request"].json;
When you need to verify the exact payload from a previous node.
console.error with process.env.NODE_ENV guard
if (process.env.NODE_ENV !== 'production') console.error(e);
Prevents leaking errors in production logs.
Unit‑test snippet in the editor
// Simulate input\nconst $item = { json: { id: 1 } };\n// Your code here
Quickly test logic without running the whole workflow.

EEFA Warning: The sandbox strips require, process, and other Node core modules. Attempting to import them will throw a “Cannot find module” error, which appears as a syntax error but is actually a security restriction.


6. Production‑Ready Checklist for Function Nodes

  • Linting enabled – no red squiggles before saving.
  • async only when needed – avoid unnecessary promises.
  • No hard‑coded secrets – use n8n credentials or environment variables.
  • Error handling – wrap external calls in try / catch.
  • Output validation – ensure the returned object matches downstream expectations (return { json: {...} } vs. plain object).
  • Remove debug console.log – replace with $node["Debug"].json in a separate test workflow.

7. Frequently Asked Questions

Q1. Why does the error point to line 1 even though my code looks fine?
A: n8n’s sandbox prepends a wrapper (module.exports = async function(){ … }). A missing closing brace in your code shifts the parser’s view, causing the wrapper’s first line to be flagged.

Q2. Can I use modern ES2022 features (e.g., optional chaining, nullish coalescing)?
A: Yes – n8n runs on Node 18, which fully supports these features. Ensure linting is set to ECMAScript 2022.

Q3. Is there a way to see the *exact* code that n8n sends to Node?
A: Open **Developer Tools → Sources → “Function node”** – you’ll see the generated wrapper with your code inserted.


9. Next Steps

  • If the Function node still fails after syntax correction, investigate runtime errors (network timeouts, invalid JSON) in the sibling pages linked above.
  • Consider extracting complex logic into a custom n8n node or an external JavaScript file for easier testing and version control.

Conclusion

Syntax errors in n8n’s Function node are almost always a matter of missing punctuation, improper await usage, or scope confusion. By enabling line numbers, linting, and the “Execute Node” button, you can pinpoint the exact line, apply the targeted fix shown in the tables, and verify success instantly. Follow the production checklist to keep your workflows secure and maintainable, and you’ll see fewer interruptions and smoother automation in real‑world deployments.

Leave a Comment

Your email address will not be published. Required fields are marked *