Registry / http-networking / vary
library1.1.2jsnpmunverified

`vary` is a focused Node.js utility library designed for manipulating the HTTP `Vary` response header. It provides a simple API to append fields to the `Vary` header of a Node.js `http.ServerResponse` object or directly to an existing `Vary` header string. Currently at version 1.1.2, the package maintains a stable and slow release cadence, primarily focusing on performance improvements and input validation rather than frequent feature additions. Its key differentiator is its singular purpose, offering a reliable and minimal solution for ensuring correct cache behavior when content varies based on request headers, without introducing additional HTTP framework overhead. It is a foundational utility used by many HTTP servers and middleware in the Node.js ecosystem.

npm install vary
INSTALL
IMPORT
SIG · VARY
V
vary
http-networkingjavascriptv1.1.2
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.

vary
const vary = require('vary')
import vary from 'vary'
This package is CommonJS-only. Direct ESM imports like `import vary from 'vary'` or `import { vary } from 'vary'` are not supported and will result in runtime errors. TypeScript users should use `import vary = require('vary');` or enable `esModuleInterop`.
vary.append
const vary = require('vary'); vary.append(header, field)
import { append } from 'vary'
`append` is a named export on the main `vary` object, not a separate top-level export. You must import the `vary` object first.

Demonstrates how to use `vary(res, field)` to add a field to the Vary header of an HTTP response and `vary.append(header, field)` to manipulate a header string.

const http = require('http'); const vary = require('vary'); http.createServer(function onRequest (req, res) { // About to user-agent sniff, so add User-Agent to Vary header vary(res, 'User-Agent'); const ua = req.headers['user-agent'] || ''; const isMobile = /mobi|android|touch|mini/i.test(ua); // Serve site, depending on isMobile res.setHeader('Content-Type', 'text/html'); res.end('You are (probably) ' + (isMobile ? '' : 'not ') + 'a mobile user'); }).listen(3000, () => { console.log('Server listening on http://localhost:3000'); }); // Example of using vary.append directly on a string const initialVaryHeader = 'Accept, Accept-Encoding'; const newVaryHeader = vary.append(initialVaryHeader, 'Origin'); console.log('Appended Vary header:', newVaryHeader); // 'Accept, Accept-Encoding, Origin'
Debug
Known issues
breakingStarting with v1.1.0, the `vary` function strictly enforces valid HTTP field names. If you pass an invalid or malformed field name, it will now be rejected, whereas previous versions might have silently ignored or attempted to normalize it, potentially leading to a valid but unintended header value.
fix
Ensure all `field` arguments passed to `vary(res, field)` or `vary.append(header, field)` conform to valid HTTP header field name syntax.
affects: >=1.1.0
gotchaThe `vary` package is a CommonJS module and does not natively support ES Modules (`import`/`export`) syntax. Attempting to import it using `import vary from 'vary'` will likely result in a runtime error.
fix
Always use `const vary = require('vary')` to import the module in both JavaScript and TypeScript projects (or configure TypeScript's `esModuleInterop` and `allowSyntheticDefaultImports` if you prefer `import` syntax).
affects: all
gotchaWhile `vary` can accept a string of multiple fields (e.g., `'Accept, User-Agent'`) or an array of fields, it's crucial to understand that it appends and deduplicates. If you provide a field already present, it maintains its position rather than moving it to the end.
fix
Understand `vary`'s behavior: it's designed to ensure a field is *present* in the `Vary` header without duplicating it or altering its existing order if already present. For complete control over header order, manipulate the string directly or use `vary.append` with an initial empty string.
affects: all
Errors
Common errors & fixes
TypeError: vary is not a function
Attempting to use `import vary from 'vary'` in an ES Module context or when `esModuleInterop` is not enabled in TypeScript.
fix
Change the import statement to `const vary = require('vary')`.
Error: Invalid field name
Passing a non-string, empty string, or syntactically invalid HTTP field name to `vary(res, field)` or `vary.append(header, field)`.
fix
Ensure the `field` argument is a non-empty string representing a valid HTTP header field name (e.g., 'User-Agent', 'Accept-Encoding'). Refer to RFC 7231 for valid field name syntax.
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
12
Amazon
1
OpenAI (training)
1
Resources