nodejs/node · error · TypeError

MemoryCacheStore options must be an object

Error message

MemoryCacheStore options must be an object

What it means

Thrown by the MemoryCacheStore constructor (undici's in-memory HTTP cache backing store) when opts is provided but typeof opts !== 'object'. The store is used by the CacheInterceptor to cache responses in process memory. Passing a string, number, array, or other non-object triggers this before any field validation.

Source

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

 */
class MemoryCacheStore extends EventEmitter {
  #maxCount = 1024
  #maxSize = 104857600 // 100MB
  #maxEntrySize = 5242880 // 5MB

  #size = 0
  #count = 0
  #entries = new Map()
  #hasEmittedMaxSizeEvent = false

  /**
   * @param {import('../../types/cache-interceptor.d.ts').default.MemoryCacheStoreOpts | undefined} [opts]
   */
  constructor (opts) {
    super()
    if (opts) {
      if (typeof opts !== 'object') {
        throw new TypeError('MemoryCacheStore options must be an object')
      }

      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

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Pass a plain object (or omit for defaults): new MemoryCacheStore({ maxCount: 1000, maxSize: 1e8 }).
  2. If config comes from JSON text, parse it first: JSON.parse(cfgStr).
  3. If you only need defaults, call new MemoryCacheStore() with no argument.

Example fix

// before
new MemoryCacheStore(configStr) // string from env

// after
new MemoryCacheStore(JSON.parse(configStr))
// or simply
new MemoryCacheStore({ maxCount: 1000 })
Defensive patterns

Strategy: validation

Validate before calling

function makeCacheStore(opts) {
  if (opts == null) return new MemoryCacheStore()
  if (typeof opts === 'string') opts = JSON.parse(opts)
  if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {
    throw new TypeError('MemoryCacheStore options must be an object')
  }
  return new MemoryCacheStore(opts)
}

Type guard

function isStoreOpts(v) { return !!v && typeof v === 'object' && !Array.isArray(v) }

Prevention

When it happens

Trigger: Constructing new MemoryCacheStore('100') or new MemoryCacheStore(100) by mistake; passing a JSON string instead of a parsed object; passing an array of config entries.

Common situations: Reading cache config from an env var or file as a string and forgetting JSON.parse; passing the wrong variable (a size number instead of an opts object); copy-paste from an example that omitted the braces.

Related errors


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