Registry / http-networking / accept-language

accept-language

JSON →
library3.0.20jsnpmunverified

accept-language is a Node.js library designed to parse the HTTP Accept-Language header, enabling applications to determine the user's preferred language based on the browser's request. It adheres strictly to the BCP47 standard for language tag compliance, ensuring robust internationalization (i18n) support. The current stable version is 3.0.20, which was last updated over two years ago, indicating a mature and stable codebase with a slow release cadence focused on maintenance rather than frequent feature additions. A key differentiator of this library is its streamlined approach: it focuses on providing a single best-matched language tag from a pre-defined list, simplifying the process for common i18n use cases where a direct match is preferred over a ranked list of possibilities. This contrasts with other parsers that might return an array of parsed language objects or offer extensive, complex matching options.

npm install accept-language
INSTALL
IMPORT
SIG · ACCEPT-LANGUAGE
A
accept-language
http-networkingjavascriptv3.0.20
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.

acceptLanguage
import acceptLanguage from 'accept-language';
import { acceptLanguage } from 'accept-language';
This package exports a default, stateful instance for convenience. Named imports are not available.
acceptLanguage
const acceptLanguage = require('accept-language');
CommonJS `require` is fully supported for Node.js environments alongside ESM.
languages
acceptLanguage.languages(['en-US', 'zh-CN']);
import { languages } from 'accept-language';
The `languages` method is accessed directly from the default-imported `acceptLanguage` instance.

Demonstrates how to initialize the parser with supported languages and then use it to find the best-matching language from an HTTP Accept-Language header.

import acceptLanguage from 'accept-language'; // Configure the supported languages for the parser instance. acceptLanguage.languages(['en-US', 'es-MX', 'fr-FR', 'en']); // Simulate an Accept-Language header from an incoming HTTP request. const clientAcceptLanguageHeader = 'fr-CA,fr;q=0.8,en-US;q=0.6,en;q=0.4,sv;q=0.2'; // Get the best-matched language based on the configured list. const matchedLanguage = acceptLanguage.get(clientAcceptLanguageHeader); console.log(`Client Accept-Language header: ${clientAcceptLanguageHeader}`); console.log(`Best matched language: ${matchedLanguage}`); // Example with a different header const anotherHeader = 'de,en;q=0.5'; const anotherMatched = acceptLanguage.get(anotherHeader); console.log(`\nAnother header: ${anotherHeader}`); console.log(`Best matched language: ${anotherMatched}`);
Debug
Known issues
gotchaThe `languages()` method must be called to set the supported language tags before `get()` can be used to perform matching. Failing to do so will result in no matches or unexpected behavior.
fix
Always call `acceptLanguage.languages(yourSupportedLanguageArray)` once during application setup or before the first call to `get()`.
affects: >=1.0.0
gotchaThis library is not designed to be used directly as an Express.js (or similar framework) middleware. For Express integration, the `express-request-language` package is specifically recommended as a dedicated middleware.
fix
If using Express, install and use `express-request-language` (`npm install express-request-language`) instead of trying to integrate `accept-language` directly into your middleware stack.
affects: >=1.0.0
gotchaLanguage tags provided to `acceptLanguage.languages()` and those in the `Accept-Language` header string should comply with the BCP47 standard for accurate matching. Invalid or malformed tags may lead to incorrect results.
fix
Ensure all language tags (e.g., 'en-US', 'fr-CA', 'zh-Hans') adhere to BCP47 specifications for optimal performance and correct language negotiation.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: acceptLanguage.get is not a function
The `acceptLanguage.languages()` method was not called prior to attempting to retrieve a matched language.
fix
Initialize the `acceptLanguage` instance with a list of supported language tags using `acceptLanguage.languages(['en-US', 'es-MX'])` before calling `get()`.
Unexpected language returned or no language matched.
The list of supported languages passed to `acceptLanguage.languages()` might not contain the expected BCP47 tags, or the incoming `Accept-Language` header is malformed.
fix
Double-check the array of language tags provided to `acceptLanguage.languages()` for correct BCP47 formatting. Also, inspect the incoming `Accept-Language` header string for any deviations from standard formatting that might hinder matching.
Upgrade
Version history
3.0.20latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
45 hits · last 30 days
node
40
OpenAI (training)
1
Resources
accept-language — npm install accept-language · libregistry