nodejs/node · error · TypeError
SqliteCacheStore options.maxCount must be a non-negative int
Error message
SqliteCacheStore options.maxCount must be a non-negative integer
What it means
Thrown when opts.maxCount is defined but is not a non-negative integer. maxCount caps how many cached responses (across all URLs) the store keeps; the constructor validates it the same way as maxEntrySize — must be a number, integral, and >= 0.
Source
Thrown at deps/undici/src/lib/cache/sqlite-cache-store.js:106
opts.maxEntrySize < 0
) {
throw new TypeError('SqliteCacheStore options.maxEntrySize must be a non-negative integer')
}
if (opts.maxEntrySize > MAX_ENTRY_SIZE) {
throw new TypeError('SqliteCacheStore options.maxEntrySize must be less than 2gb')
}
this.#maxEntrySize = opts.maxEntrySize
}
if (opts.maxCount !== undefined) {
if (
typeof opts.maxCount !== 'number' ||
!Number.isInteger(opts.maxCount) ||
opts.maxCount < 0
) {
throw new TypeError('SqliteCacheStore options.maxCount must be a non-negative integer')
}
this.#maxCount = opts.maxCount
}
}
if (!DatabaseSync) {
DatabaseSync = require('node:sqlite').DatabaseSync
}
this.#db = new DatabaseSync(opts?.location ?? ':memory:')
this.#db.exec(`
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
PRAGMA temp_store = memory;
PRAGMA optimize;
CREATE TABLE IF NOT EXISTS cacheInterceptorV${VERSION} (
-- Data specific to usView on GitHub (pinned to 1b2de5e052)
Solutions
- Pass an integer: new SqliteCacheStore({ maxCount: 1000 }).
- Coerce from config: Number.parseInt(process.env.CACHE_MAX_COUNT, 10).
- Omit maxCount to keep the default cap.
- Validate the value is finite and integral before passing.
Example fix
// before
new SqliteCacheStore({ maxCount: process.env.CACHE_MAX_COUNT })
// after
new SqliteCacheStore({ maxCount: Number.parseInt(process.env.CACHE_MAX_COUNT, 10) }) Defensive patterns
Strategy: validation
Validate before calling
function coerceMaxCount(v) {
const n = Number(v)
if (!Number.isInteger(n) || n < 0) {
throw new TypeError('maxCount must be a non-negative integer')
}
return n
} Type guard
function isValidMaxCount(v) {
return typeof v === 'number' && Number.isInteger(v) && v >= 0
} Prevention
- Parse env vars with Number.parseInt(..., 10).
- Omit the option to keep the default cap.
- Validate the parsed value before constructing the store.
- Unit-test with string, float, and negative inputs.
When it happens
Trigger: Passing maxCount as a string ('100'), a float (1.5), a negative number, NaN, or Infinity. Also when a config layer returns undefined-as-string or a BigInt.
Common situations: Pulling maxCount from an env var without parseInt; using Number.MAX_SAFE_INTEGER as a stand-in for unlimited (that actually works but is a code smell); mixing up maxCount with maxEntrySize.
Related errors
- SqliteCacheStore options.maxEntrySize must be a non-negative
- SqliteCacheStore options must be an object
- SqliteCacheStore options.maxEntrySize must be less than 2gb
- expected key to be object, got ${typeof key}
- MemoryCacheStore options must be an object
AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13).
Data as JSON: /api/errors/b46845a8f9fe5a2b.
Report an issue: GitHub.