Registry / web-framework / nocache

nocache

JSON →
library4.0.0jsnpmunverified

The `nocache` package is an Express/Connect middleware designed to aggressively disable client-side caching by setting specific HTTP response headers. It is currently at stable version 4.0.0. As a component of the Helmet.js suite, its release cadence is generally tied to Helmet's, focusing on stability and security rather than frequent feature additions, with major version updates typically aligning with Node.js EOLs or significant breaking changes. Its primary differentiator is its simplicity and integration within the broader Helmet.js ecosystem, providing a straightforward, opinionated way to ensure clients always request fresh resources. This is crucial for applications sensitive to stale data, enforcing immediate updates, or handling sensitive information that should never reside in a browser cache. It effectively sets `Cache-Control`, `Expires`, and `Surrogate-Control` headers.

npm install nocache
INSTALL
IMPORT
SIG · NOCACHE
N
nocache
web-frameworkjavascriptv4.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

nocache
import nocache from 'nocache';
const nocache = require('nocache'); // CommonJS is still supported but ESM is preferred in modern applications.
The `nocache` export is a function that returns the middleware. It must be called (`nocache()`) before being passed to `app.use()`.
nocache
const nocache = require('nocache');
import nocache from 'nocache'; // While possible, for CJS-only projects, 'require' is the correct syntax.
For CommonJS environments, `require` is the standard way to import. Ensure you still call it as a function: `app.use(nocache());`.

Demonstrates how to initialize an Express application and apply the `nocache` middleware globally, ensuring all HTTP responses include headers designed to disable client-side caching.

import express from 'express'; import nocache from 'nocache'; const app = express(); const port = process.env.PORT ?? 3000; // Apply the nocache middleware globally to all routes. // This ensures that all responses from this server attempt to disable client-side caching. app.use(nocache()); app.get('/', (req, res) => { res.send('<h1>Welcome!</h1><p>This content should not be cached by your browser.</p>'); }); app.get('/api/data', (req, res) => { // Even API endpoints will have caching headers applied, forcing fresh requests. res.json({ message: 'Dynamic data from API', timestamp: new Date().toISOString() }); }); app.listen(port, () => { console.log(`Nocache server listening on port ${port}`); console.log('Inspect network requests in your browser to see Cache-Control, Expires, and Surrogate-Control headers set to disable caching.'); });
Debug
Known issues
breakingVersion 4.0.0 of `nocache` updated its minimum Node.js engine requirement to `>=16.0.0`. Older Node.js versions are not officially supported and may encounter issues.
fix
Upgrade your Node.js environment to version 16.0.0 or higher to ensure compatibility and access the latest features and security updates.
affects: >=4.0.0
gotchaApplying `nocache` globally to an Express application can have significant performance implications for your users and server. By disabling all client-side caching, every asset (HTML, CSS, JS, images) must be re-downloaded on each request, increasing network traffic and server load.
fix
Carefully consider where `nocache` is applied. For static assets or content that changes infrequently, consider applying `nocache` only to specific routes (e.g., `app.get('/admin', nocache(), ...)`) or using more granular cache control strategies.
affects: >=1.0.0
gotchaThe `nocache` middleware sets specific HTTP headers (`Cache-Control`, `Expires`, `Surrogate-Control`). If other middleware, your web server (e.g., Nginx, Apache), or a reverse proxy also manipulates caching headers, there might be conflicts or unintended behavior where `nocache`'s headers are overwritten or fail to take effect as expected.
fix
Review the order of middleware in your application and the configuration of your web server/proxy. Ensure `nocache` is applied at a point where its headers will not be overridden by subsequent processes if you intend for it to be the authoritative cache control mechanism.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: nocache is not a function
This error typically occurs when `nocache` is passed directly to `app.use()` without being called as a function, e.g., `app.use(nocache);`.
fix
The `nocache` export is a factory function that returns the actual middleware. You must call it, even with no arguments, before passing its result to `app.use()`: `app.use(nocache());`.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
12
Amazon
1
OpenAI (training)
1
Resources
nocache — npm install nocache · libregistry