Node execution failed

I’m getting this errore in a Code node

Node execution failed

This can happen for various reasons. Please try executing the node again. If the problem persists, you can try the following:

  1. Reduce the number of items processed at a time, by batching them using a loop node
  2. Upgrade your cloud plan to increase the available memory
    It’s a script dividing text into chunks.

The script is this:

// Parametri configurabili
const maxChars = 1000;      // lunghezza massima (in caratteri) per chunk
const overlapChars = 200;   // caratteri di sovrapposizione tra chunk

// Estrai il testo dall'input
const text = $('Extract from File').first().json.text;

const chunks = [];
let start = 0;
const totalLength = text.length;

while (start < totalLength) {
  // posizione massima teorica
  let tentativeEnd = Math.min(start + maxChars, totalLength);

  // Se non siamo alla fine, cerchiamo di non tagliare parole o frasi
  if (tentativeEnd < totalLength) {
    // cerca l'ultima fine frase (.
    const endOfSentence = text.lastIndexOf(/[\.\!\?]\s/, tentativeEnd);
    if (endOfSentence > start + overlapChars) {
      tentativeEnd = endOfSentence + 1; // includi il carattere di punteggiatura
    } else {
      // altrimenti, cerca l'ultimo spazio
      const lastSpace = text.lastIndexOf(' ', tentativeEnd);
      if (lastSpace > start + overlapChars) {
        tentativeEnd = lastSpace;
      }
    }
  }

  const chunkText = text.slice(start, tentativeEnd).trim();
  chunks.push(chunkText);

  // Calcola il nuovo start tenendo conto dell'overlap
  start = tentativeEnd - overlapChars;
  if (start < 0) start = 0;
}

// Restituisci ogni chunk come singolo item JSON
return chunks.map((chunk, index) => ({
  json: {
    chunk_id: index,
    text: chunk,
  }
}));

The text is 100k characters long.

Here’s the scenario:

Is it a memory problem?

1 Like

Hi @albertocabasvidani

Can you put your workflow in a code block so it will render here?

Here’s a video on how to do that.

Best,

Robert
pasteworkflow

How much data are you looking to process? You may also want to look into self hosting where you can control the memory available.

Here’s a link on memory from the docs. Memory-related errors | n8n Docs

Also check out this link on memory availability.

Also, u could try check the /metrics endpoint, I made a dashboard which u. can see stats for this.

This could help, but u can also use console.log to print.

You could also try limit the amount you process see if this changes and stops the crashing of the node.

also after a quick look at the code node,

this const endOfSentence = text.lastIndexOf(/[.!?]\s/, tentativeEnd);

may not place nicely in n8n and you could try using

with .slice() and .match() instead of lastIndexOf()

Hope this helps

Done, sorry!

Thanks, it’s for a client. Self-hosting for now isn’t a solution.

Thanks.
Why does this not play nicely?