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
- Pass a non-negative integer byte count: { maxSize: 100 * 1024 * 1024 }.
- Compute sizes with integer arithmetic and validate with Number.isInteger.
- 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
- Express sizes in integer bytes (use integer MB * 1024 * 1024).
- Validate Number.isInteger before assigning maxSize.
- Omit maxSize for the default 100 MB rather than passing Infinity.
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
- MemoryCacheStore options must be an object
- MemoryCacheStore options.maxCount must be a non-negative int
- MemoryCacheStore options.maxEntrySize must be a non-negative
- expected ${name} to be an array or undefined, got ${typeof o
- expected ${name}[${i}] to be a string or RegExp, got ${typeo
AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13).
Data as JSON: /api/errors/949920633111445d.
Report an issue: GitHub.