{"record":{"id":"f40189cfedac78f3","repo":"n8n-io/n8n","slug":"chunk-expected-non-zero-numeric-arg-e-g-chun","errorCode":null,"errorMessage":"chunk(): expected non-zero numeric arg, e.g. .chunk(5)","messagePattern":"chunk\\(\\): expected non-zero numeric arg, e\\.g\\. \\.chunk\\(5\\)","errorType":"exception","errorClass":"ExpressionExtensionError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/expression-runtime/src/extensions/array-extensions.ts","lineNumber":180,"sourceCode":"\t\tthrow new ExpressionExtensionError(\n\t\t\t'smartJoin(): expected two string args, e.g. .smartJoin(\"name\", \"value\")',\n\t\t);\n\t}\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-return\n\treturn value.reduce<any>((o, v) => {\n\t\tif (typeof v === 'object' && v !== null && keyField in v && valueField in v) {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any\n\t\t\to[(v as any)[keyField]] = (v as any)[valueField];\n\t\t}\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-return\n\t\treturn o;\n\t}, {});\n}\n\nfunction chunk(value: unknown[], extraArgs: number[]) {\n\tconst [chunkSize] = extraArgs;\n\tif (typeof chunkSize !== 'number' || chunkSize === 0) {\n\t\tthrow new ExpressionExtensionError('chunk(): expected non-zero numeric arg, e.g. .chunk(5)');\n\t}\n\tconst chunks: unknown[][] = [];\n\tfor (let i = 0; i < value.length; i += chunkSize) {\n\t\tchunks.push(value.slice(i, i + chunkSize));\n\t}\n\treturn chunks;\n}\n\nfunction renameKeys(value: unknown[], extraArgs: string[]): unknown[] {\n\tif (extraArgs.length === 0 || extraArgs.length % 2 !== 0) {\n\t\tthrow new ExpressionExtensionError(\n\t\t\t'renameKeys(): expected an even amount of args: from1, to1 [, from2, to2, ...]. e.g. .renameKeys(\"name\", \"title\")',\n\t\t);\n\t}\n\treturn value.map((v) => {\n\t\tif (typeof v !== 'object' || v === null) {\n\t\t\treturn v;\n\t\t}","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/expression-runtime/src/extensions/array-extensions.ts#L162-L198","documentation":"Thrown as ExpressionExtensionError by the .chunk() array extension when chunkSize is not a number or is exactly zero. chunk() splits the array into sub-arrays of length chunkSize; a zero or non-numeric size is invalid (zero would also cause an infinite loop in the for-loop, so it is rejected up front).","triggerScenarios":"Calling .chunk() with no argument, a string, or 0. Examples: [1,2,3].chunk() (missing), [1,2,3].chunk('2') (string), [1,2,3].chunk(0) (zero). Negative sizes would pass the type check but the loop degrades; the guard focuses on the dangerous zero/non-number cases.","commonSituations":"Passing a dynamic chunk size that resolved to undefined or a string from upstream data. Hardcoding 0 by mistake. A template variable for chunk size that wasn't converted to a number.","solutions":["Pass a positive integer: .chunk(5).","If the size is dynamic, coerce and validate it first: .chunk(Number(size)).","Guard against zero/undefined before calling: only call chunk when size is a positive number.","Confirm the upstream expression that produces chunkSize yields a number."],"exampleFix":"// before — chunk size from upstream data is a string or undefined\n{{ $json.items.chunk($json.size) }}\n// after — coerce to a positive integer\n{{ $json.items.chunk(Number($json.size)) }}","handlingStrategy":"validation","validationCode":"function chunkSafe(arr, size) {\n  if (typeof size !== 'number' || size === 0) throw new TypeError('chunk(): expected non-zero numeric arg');\n  const out = []; for (let i = 0; i < arr.length; i += size) out.push(arr.slice(i, i + size)); return out;\n}","typeGuard":"function isValidChunkSize(size) { return typeof size === 'number' && size !== 0; }","tryCatchPattern":"try {\n  result = arr.chunk(size);\n} catch (e) {\n  if (e.name === 'ExpressionExtensionError' && /chunk/i.test(e.message)) {\n    // ensure size is a non-zero number: arr.chunk(Number(size) || 1)\n  }\n}","preventionTips":["Pass a positive integer chunk size: .chunk(5).","Coerce dynamic sizes: .chunk(Number(size)).","Guard against zero/undefined before calling: isValidChunkSize(size).","Confirm upstream expressions that produce the size yield a number."],"tags":["expression-runtime","array-extensions","chunk","argument-validation"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}