nodejs/node · error · TypeError

MemoryCacheStore options.maxEntrySize must be a non-negative

Error message

MemoryCacheStore options.maxEntrySize must be a non-negative integer

What it means

Thrown by the MemoryCacheStore constructor when opts.maxEntrySize is defined but is not a non-negative integer. maxEntrySize caps the size of a single cached entry in bytes (default 5 MB / 5242880); entries larger than this are not cached. Floats, negatives, strings, NaN, and Infinity are rejected.

Source

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

      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
      }
    }
  }

  /**
   * Get the current size of the cache in bytes
   * @returns {number} The current size of the cache in bytes
   */
  get size () {
    return this.#size
  }

  /**
   * Check if the cache is full (either max size or max count reached)
   * @returns {boolean} True if the cache is full, false otherwise
   */

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Pass a non-negative integer byte count: { maxEntrySize: 5 * 1024 * 1024 }.
  2. Validate parsed config with Number.isInteger before assigning.
  3. Omit maxEntrySize if you want the default per-entry cap or no cap beyond maxSize.

Example fix

// before
new MemoryCacheStore({ maxEntrySize: 2.5 * 1024 * 1024 }) // float

// after
new MemoryCacheStore({ maxEntrySize: Math.floor(2.5 * 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({ maxEntrySize: optInt(config.entryBytes) })

Type guard

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

Prevention

When it happens

Trigger: Passing maxEntrySize: 2.5e6 (float), maxEntrySize: -1, maxEntrySize: '5242880' (string), or maxEntrySize: Infinity.

Common situations: Setting a per-entry cap computed from MB with a fractional multiplier; env-string config not parsed; using Infinity (disallowed — omit to allow any size up to maxSize).

Related errors


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