nodejs/node · error · TypeError

MemoryCacheStore options.maxSize must be a non-negative inte

Error message

MemoryCacheStore options.maxSize must be a non-negative integer

What it means

Thrown by the MemoryCacheStore constructor when opts.maxSize is defined but is not a non-negative integer. maxSize caps the total cached body size in bytes (default 100 MB / 104857600). Floats, negatives, strings, NaN, and Infinity are rejected.

Source

Thrown at deps/undici/src/lib/cache/memory-cache-store.js:55

      if (opts.maxCount !== undefined) {
        if (
          typeof opts.maxCount !== 'number' ||
          !Number.isInteger(opts.maxCount) ||
          opts.maxCount < 0
        ) {
          throw new TypeError('MemoryCacheStore options.maxCount must be a non-negative integer')
        }
        this.#maxCount = opts.maxCount
      }

      if (opts.maxSize !== undefined) {
        if (
          typeof opts.maxSize !== 'number' ||
          !Number.isInteger(opts.maxSize) ||
          opts.maxSize < 0
        ) {
          throw new TypeError('MemoryCacheStore options.maxSize must be a non-negative integer')
        }
        this.#maxSize = opts.maxSize
      }

      if (opts.maxEntrySize !== undefined) {
        if (
          typeof opts.maxEntrySize !== 'number' ||
          !Number.isInteger(opts.maxEntrySize) ||
          opts.maxEntrySize < 0
        ) {
          throw new TypeError('MemoryCacheStore options.maxEntrySize must be a non-negative integer')
        }
        this.#maxEntrySize = opts.maxEntrySize
      }
    }
  }

  /**

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Pass a non-negative integer byte count: { maxSize: 100 * 1024 * 1024 }.
  2. Compute sizes with integer arithmetic and validate with Number.isInteger.
  3. For 'unlimited', omit maxSize.

Example fix

// before
new MemoryCacheStore({ maxSize: '104857600' })

// after
new MemoryCacheStore({ maxSize: 100 * 1024 * 1024 })
Defensive patterns

Strategy: validation

Validate before calling

function optInt(v) {
  const n = Number(v)
  if (!Number.isInteger(n) || n < 0) throw new TypeError('must be a non-negative integer')
  return n
}
// new MemoryCacheStore({ maxSize: optInt(config.sizeBytes) })

Type guard

function isNonNegInt(v) { return Number.isInteger(v) && v >= 0 }

Prevention

When it happens

Trigger: Passing maxSize: 50 * 1024 * 1024.5 (float bytes), maxSize: -1, maxSize: '104857600' (string), or maxSize: Infinity.

Common situations: Configuring size in MB and multiplying by a float KB factor; reading from env as a string; using Infinity for unlimited (disallowed — omit instead).

Related errors


AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13). Data as JSON: /api/errors/949920633111445d. Report an issue: GitHub.