금. 8μ›” 15th, 2025

D: Are you ready to unlock the full potential of n8n’s Code Node? 🀩 Whether you’re automating workflows or processing complex data, the Code Node is your Swiss Army knife. But mastering it requires knowing key syntax and debugging tricks. Let’s dive in!


πŸ”Ή Why Use the Code Node?

The Code Node lets you write custom JavaScript/Python snippets to:

  • Transform data πŸ› οΈ (e.g., JSON parsing, calculations).
  • Call APIs dynamically 🌐.
  • Implement logic that isn’t available in standard nodes.

Example:

// Convert Celsius to Fahrenheit  
const celsius = $input.item.json.temperature;  
return { fahrenheit: celsius * 9/5 + 32 };  

πŸ“Œ Must-Know Syntax

1️⃣ Accessing Input Data

  • Use $input.item.json for JSON data from previous nodes.
  • For binary data (e.g., images), use $input.item.binary.

Example:

const userEmail = $input.item.json.email;  
return { filteredEmail: userEmail.split('@')[0] };  

2️⃣ Returning Output

  • Return an object (return { key: value }) to pass data to the next node.
  • Use return [ { item1 }, { item2 } ] for multiple items.

Example:

return {  
  fullName: `${$input.item.json.firstName} ${$input.item.json.lastName}`,  
  userId: Math.random().toString(36).substring(2)  
};  

3️⃣ Using Libraries (JavaScript Only)

  • Pre-installed libraries: moment (dates), lodash (utilities).

Example:

const _ = require('lodash');  
return _.uniq($input.item.json.emails); // Remove duplicates  

🐞 Debugging Like a Pro

πŸ”Έ 1. Use console.log()

  • Check values in the “Debug” tab of the Code Node.
console.log("Raw Input:", $input.item.json); // Debug output  

πŸ”Έ 2. Handle Errors Gracefully

Wrap code in try-catch to avoid workflow crashes.

try {  
  const data = $input.item.json.nonExistentField; // Risky!  
  return { result: data * 2 };  
} catch (error) {  
  return { error: error.message };  
}  

πŸ”Έ 3. Validate Data Structure

  • Use if checks to avoid “undefined” errors.
if ($input.item.json?.address?.city) {  
  return { city: $input.item.json.address.city };  
} else {  
  return { city: "Unknown" };  
}  

πŸ’‘ Bonus Tips

βœ… Test Small Snippets First: Isolate code in the “Code” tab before integrating.
βœ… Leverage AI Assist: n8n’s AI Code Node suggests fixes!
βœ… Use $node for Cross-Node Data:

const priorData = $node["Webhook"].json.response;  

🎯 Final Thoughts

The Code Node is powerful but requires precision. Master these syntax rules and debugging tricks to build error-proof automations! πŸš€

> Got stuck? Drop a comment below! πŸ‘‡ #n8n #Automation #LowCode


Would you like a deep dive into Python in Code Node next? Let me know! 😊

λ‹΅κΈ€ 남기기

이메일 μ£Όμ†ŒλŠ” κ³΅κ°œλ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. ν•„μˆ˜ ν•„λ“œλŠ” *둜 ν‘œμ‹œλ©λ‹ˆλ‹€