expressjs/multer · error · TypeError

Expected limits.' + key + ' to be a non-negative integer or

Error message

Expected limits.' + key + ' to be a non-negative integer or Infinity

What it means

This TypeError is thrown by the limits validation helper when a value passed in the `limits` options object is not a non-negative integer or Infinity. The library requires every provided limit (e.g. concurrency, queue limits) to be a whole number >= 0 or Infinity so downstream counting logic cannot break; null/undefined values are allowed (treated as unset), but anything else fails fast at configuration time rather than causing subtle runtime bugs.

Source

Thrown at lib/validate-limits.js:10

// busboy compares most limits with strict equality, so a non-integer value
// never matches and silently disables the limit. Reject such values up front.
function validateLimits (limits) {
  Object.keys(limits).forEach(function (key) {
    var value = limits[key]

    if (value == null) return
    if ((Number.isInteger(value) && value >= 0) || value === Infinity) return

    throw new TypeError('Expected limits.' + key + ' to be a non-negative integer or Infinity')
  })
}

module.exports = validateLimits

View on GitHub (pinned to a53296bbd6)

Solutions

  1. Convert any string values from env vars/CLI args to numbers with Number() and validate with Number.isInteger before passing them.
  2. Replace negative sentinel values like -1 with Infinity to mean unlimited.
  3. Check for non-integer values (decimals, NaN) and round, floor, or fix the source value.
  4. Remove keys with invalid values if the limit is intentionally unset (null/undefined are accepted).

Example fix

// before
const pool = createPool({ limits: { concurrency: process.env.CONCURRENCY } })
// after
const concurrency = Number(process.env.CONCURRENCY)
const pool = createPool({ limits: { concurrency: concurrency > 0 ? concurrency : Infinity } })
Defensive patterns

Strategy: validation

Validate before calling

function validateLimits(limits) {
  for (const [key, value] of Object.entries(limits)) {
    if (value == null) continue
    if ((Number.isInteger(value) && value >= 0) || value === Infinity) continue
    throw new TypeError('Expected limits.' + key + ' to be a non-negative integer or Infinity')
  }
}

Type guard

function isValidLimit(value) {
  return value == null || (Number.isInteger(value) && value >= 0) || value === Infinity
}

Try / catch

try {
  createPool({ limits })
} catch (err) {
  if (err instanceof TypeError && err.message.startsWith('Expected limits.')) {
    console.error('Invalid limits config:', err.message)
    process.exit(1)
  }
  throw err
}

Prevention

When it happens

Trigger: Calling the module's constructor/setup with a `limits` object containing a key whose value is a negative integer (e.g. -1), a non-integer number (e.g. 1.5, NaN), a numeric string (e.g. '10' from a CLI arg or env var), a boolean, an object, or a value parsed incorrectly from JSON.

Common situations: Passing process.env values (strings) directly into limits without Number() parsing; using parseInt on malformed input yielding NaN; defaulting limits to -1 to mean 'unlimited' instead of Infinity; copying limits from another library whose sentinel for unlimited is -1 or 0.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of expressjs/multer@a53296bbd6 (2026-09-01). Data as JSON: /api/errors/703bc329ddb822a7. Report an issue: GitHub.