Registry / storage / cache-key

cache-key

JSON →
library1.0.0jsnpmunverified

cache-key is a lightweight library for creating stable, deterministic cache keys from complex JavaScript objects. Version 1.0.0 targets Node.js >=16, ships TypeScript definitions, and provides a single function `hashCacheKey` that returns a JSON-stringified representation, sorting object keys alphabetically for consistency. Unlike JSON.stringify or libraries like object-hash, cache-key focuses on simplicity: it does not hash into short strings but retains the structure as a string for readability. Ideal for memoization and simple caching scenarios where inputs are arrays of mixed values and keys must be order-aware but property-order-independent.

npm install cache-key
INSTALL
IMPORT
SIG · CACHE-KEY
C
cache-key
storagejavascriptv1.0.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.

hashCacheKey
import { hashCacheKey } from 'cache-key'
const hashCacheKey = require('cache-key')
ESM-only since v1. CommonJS require will not work as this package is type: module.
hashCacheKey (default import)
import hashCacheKey from 'cache-key'
import { default } from 'cache-key'
The library also exports a default export equal to the named export. Both are valid.
hashCacheKey (type)
import type { hashCacheKey } from 'cache-key'
import { HashCacheKey } from 'cache-key'
Type import for type-checking only. The library exports a function, not a type.

Generates stable cache keys from an array of values, handling nested objects with sorted keys.

import { hashCacheKey } from 'cache-key'; const key1 = hashCacheKey(['user', { id: 42, role: 'admin' }]); const key2 = hashCacheKey(['user', { role: 'admin', id: 42 }]); console.log(key1); // '["user",{"id":42,"role":"admin"}]' console.log(key2); // '["user",{"id":42,"role":"admin"}]' console.log(key1 === key2); // true
Debug
Known issues
gotchaOrder of items in the input array is preserved – this is intentional but may be surprising if you expect sorted arrays.
fix
Sort the array manually before calling hashCacheKey if order-independent keys are desired.
affects: >=1.0
gotchaThe output is a JSON string, not a short hash. This may lead to long keys in storage or memory-heavy usage.
fix
Consider passing the result through a hashing function like crypto.createHash('sha256') for shorter keys.
affects: >=1.0
gotchaOnly plain objects with string keys are supported for alphabetical sorting. Arrays and primitives are untouched.
fix
Ensure objects passed in have enumerable string keys. Symbols or non-enumerable properties are ignored.
affects: >=1.0
breakinghashCacheKey does not handle circular references – will throw a TypeError.
fix
Use a library like 'json-stable-stringify' with circular reference support or remove circular references before calling.
affects: >=1.0
Errors
Common errors & fixes
TypeError: Converting circular structure to JSON
Input object contains circular references; hashCacheKey uses JSON.stringify internally.
fix
Remove or replace circular references before passing to hashCacheKey, e.g., by using a deep clone library that handles circulars.
import { hashCacheKey } from 'cache-key' / SyntaxError: Cannot use import statement outside a module
The package is ESM-only; require() is not supported.
fix
Add "type": "module" to your package.json or use dynamic import with await import('cache-key').
TypeError: hashCacheKey is not a function (when using default import with named import style)
Attempting to use default import { hashCacheKey } from 'cache-key' when the module also exports default as a named export.
fix
Use default import: import hashCacheKey from 'cache-key' or named import: import { hashCacheKey } from 'cache-key'.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
31 hits · last 30 days
node
30
Resources
cache-key — npm install cache-key · libregistry