Registry / devops / cache-content-type

cache-content-type

JSON →
library2.1.0jsnpmunverified

A Node.js library that creates a full Content-Type header string from a MIME type or file extension, with LRU caching of results. Version 1.0+ uses the same underlying database as mime-types but adds an in-memory cache (default cache size 100) to avoid repeated lookups. Release cadence is low, updated primarily for dependency bumps and Node.js version support. Differentiator: provides cached content-type lookups, improving performance when the same extensions are queried multiple times (e.g., in HTTP servers).

npm install cache-content-type
INSTALL
IMPORT
SIG · CACHE-CONTENT-TYPE
C
cache-content-type
devopsjavascriptv2.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

getType
import { getType } from 'cache-content-type';
const getType = require('cache-content-type');
ESM-only since v1.0. Default export removed; use named import.
getType
const { getType } = await import('cache-content-type');
const contentType = require('cache-content-type');
Dynamic import for CommonJS context – named destructuring required.
contentType (deprecated)
import { getType } from 'cache-content-type';
import contentType from 'cache-content-type';
In v1.0.0, the API was renamed from default export contentType to named export getType. The old default export is removed.

Shows import, type lookup with extension, MIME type, and performance of cache.

import { getType } from 'cache-content-type'; const result1 = getType('html'); console.log(result1); // 'text/html; charset=utf-8' const result2 = getType('.json'); console.log(result2); // 'application/json; charset=utf-8' const result3 = getType('application/json'); console.log(result3); // 'application/json; charset=utf-8' // Invalid MIME returns false const result4 = getType('invalid'); console.log(result4); // false // Repeated lookups use cache const start = Date.now(); for (let i = 0; i < 10000; i++) { getType('html'); } console.log(`Cached: ${Date.now() - start}ms`);
Debug
Known issues
breakingv1.0.0 removed default export; use named export 'getType'.
fix
Replace 'import contentType from "cache-content-type"' with 'import { getType } from "cache-content-type"'.
affects: >=1.0.0
breakingv2.0.0 drops support for Node.js <18.0.0.
fix
Upgrade Node.js to >=18.0.0 or stick with v1.x.
affects: >=2.0.0
gotchaCache size is fixed at 100 entries (LRU) and not configurable; may cause cache churn if many distinct extensions are used.
fix
For larger caches, consider using mime-types directly or roll your own caching layer.
affects: all
deprecatedThe package was previously named 'cache-content-type' on npm; no rename deprecation, but old documentation may refer to an earlier API.
fix
Check documentation for current usage.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (0 , cache_content_type.getType) is not a function
Using default import after v1.0.0; the package no longer has a default export.
fix
Change to named import: import { getType } from 'cache-content-type';
Cannot find module 'cache-content-type'
Not installed or wrong import path (e.g., requiring from wrong location).
fix
Run 'npm install cache-content-type' and ensure the module is in node_modules.
Expected a string but got false
Passing an unknown extension or MIME type that returns false.
fix
Check the return value: if getType returns false, handle gracefully (e.g., default to 'application/octet-stream').
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
mime-typesrequiredUsed as the underlying content-type lookup and charset detection engine.
lru-cacherequiredProvides the LRU cache implementation, version 7 or higher.
Agent activity
2 hits · last 30 days
node
2
Resources
cache-content-type — npm install cache-content-type · libregistry