nodejs/node · error · TypeError
expected opts.type to be shared, private, or undefined, got
Error message
expected opts.type to be shared, private, or undefined, got ${typeof type} What it means
Thrown by the cache() interceptor factory when opts.type is present but is neither 'shared' nor 'private'. type controls shared vs private cache semantics (RFC 9111): 'shared' treats the cache like a shared proxy, 'private' like a single-user cache. Any other value (including typos, 'public', or booleans) is rejected; omit the option to use the default 'shared'.
Source
Thrown at deps/undici/src/lib/interceptor/cache.js:523
cacheByDefault = undefined,
type = 'shared',
origins = undefined
} = opts
if (typeof opts !== 'object' || opts === null) {
throw new TypeError(`expected type of opts to be an Object, got ${opts === null ? 'null' : typeof opts}`)
}
assertCacheStore(store, 'opts.store')
assertCacheMethods(methods, 'opts.methods')
assertCacheOrigins(origins, 'opts.origins')
if (typeof cacheByDefault !== 'undefined' && typeof cacheByDefault !== 'number') {
throw new TypeError(`expected opts.cacheByDefault to be number or undefined, got ${typeof cacheByDefault}`)
}
if (typeof type !== 'undefined' && type !== 'shared' && type !== 'private') {
throw new TypeError(`expected opts.type to be shared, private, or undefined, got ${typeof type}`)
}
const globalOpts = {
store,
methods,
cacheByDefault,
type
}
const safeMethodsToNotCache = []
for (let i = 0; i < util.safeHTTPMethods.length; i++) {
const method = util.safeHTTPMethods[i]
if (!arrayIncludes(methods, method)) {
safeMethodsToNotCache.push(method)
}
}
return dispatch => {View on GitHub (pinned to 1b2de5e052)
Solutions
- Use exactly 'shared' or 'private', or omit the option for the default 'shared'.
- Normalize env-sourced values: lowercase and validate against the allow-list.
- Remember: 'private' means browser/single-user semantics; there is no 'public' option here.
Example fix
// before
client.compose(interceptors.cache({ type: 'public' }))
// after
client.compose(interceptors.cache({ type: 'private' }))
// or rely on the default
client.compose(interceptors.cache()) Defensive patterns
Strategy: validation
Validate before calling
function resolveCacheType(v) {
if (v == null) return undefined
const s = String(v).toLowerCase()
return s === 'shared' || s === 'private' ? s : undefined
} Type guard
function isCacheType(v) { return v == null || v === 'shared' || v === 'private' } Try / catch
try { client.compose(interceptors.cache({ type })) } catch (e) { if (e instanceof TypeError && /opts\.type/.test(e.message)) { client.compose(interceptors.cache({ type: resolveCacheType(type) })) } else throw e } Prevention
- Use exactly 'shared' or 'private'.
- There is no 'public' value; omit for the 'shared' default.
- Lowercase + validate env-sourced values.
When it happens
Trigger: Passing type: 'public' (common confusion with private), type: 'PRIVATE' (case sensitivity), type: true, or any other invalid value.
Common situations: Misremembering the option name (public vs private); uppercase config values from env; copy-pasting a value from a different caching library.
Related errors
- expected ${name} to be an array or undefined, got ${typeof o
- expected ${name}[${i}] to be a string or RegExp, got ${typeo
- expected type of opts to be an Object, got ${opts === null ?
- expected opts.cacheByDefault to be number or undefined, got
- expected type of opts to be an Object, got ${opts === null ?
AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13).
Data as JSON: /api/errors/11d01211383cf510.
Report an issue: GitHub.