Registry / security / jwt-cache

jwt-cache

JSON →
library1.1.0jsnpmunverified

Minimal caching wrapper for async JSON Web Token generation functions. Current version 1.1.0. Maintained by ForbesLindesay with Rolling Versions release automation. Caches tokens based on their 'exp' claim, with configurable minimum validity and optional eager refresh. Ships TypeScript types. Distinguishes itself by its simplicity and direct integration with libraries like jsonwebtoken.

npm install jwt-cache
INSTALL
IMPORT
SIG · JWT-CACHE
J
jwt-cache
securityjavascriptv1.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.

jwtCache
import jwtCache from 'jwt-cache'
const jwtCache = require('jwt-cache')
Default import; ESM-only since Node >=14.
JwtCacheOptions
import { JwtCacheOptions } from 'jwt-cache'
import JwtCacheOptions from 'jwt-cache'
Named export for TypeScript type; not available in CJS.
require (CJS)
const jwtCache = require('jwt-cache').default
const jwtCache = require('jwt-cache')
CJS users must access .default as the package is ESM-first.

Shows basic usage of jwtCache to cache a token generated by jsonwebtoken.sign with a 3-second expiry and 100ms minimum validity.

import jwtCache from 'jwt-cache'; import { sign } from 'jsonwebtoken'; const cache = jwtCache({ getToken: async () => sign({ hello: 'world' }, process.env.JWT_SECRET ?? 'secret', { expiresIn: 3 }), minimumValidityMilliseconds: 100, }); cache.getToken().then(token => console.log(token));
Debug
Known issues
deprecatedThe package does not support CommonJS require without .default
fix
Use const jwtCache = require('jwt-cache').default or switch to ESM imports.
affects: >=1.0.0
gotchaminimumValidityMilliseconds defaults to 1000, which may be too short for systems with clock skew.
fix
Set a larger value based on your network latency and clock tolerance.
affects: >=1.0.0
gotchaeagerRefreshMilliseconds should be less than minimumValidityMilliseconds to avoid constant refresh.
fix
Ensure eagerRefreshMilliseconds < minimumValidityMilliseconds.
affects: >=1.0.0
breakingVersion 1.0.0 switched from CommonJS to ESM-only; require() no longer works without .default.
fix
Use ESM imports or add .default in require.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: jwtCache is not a function
Using CJS require without .default on an ESM-only package.
fix
const jwtCache = require('jwt-cache').default
Error: 'exp' claim missing. Token must have an 'exp' claim for caching.
The getToken function returns a token without an 'exp' field.
fix
Ensure getToken signs a token with an 'expiresIn' or explicit 'exp' claim.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources
jwt-cache — npm install jwt-cache · libregistry