{"id":17149,"library":"accept-language-parser","title":"Accept Language Parser","description":"The `accept-language-parser` package provides utilities for parsing the `Accept-Language` HTTP header, which clients send to indicate their preferred human languages. It processes the header string and produces an array of language objects, each containing a `code`, optional `region`, and `quality` value, sorted in descending order of quality. Additionally, it offers a `pick` function to determine the best matching language from a list of supported languages, with an optional 'loose' matching mode. The current stable version is 1.5.0. This package appears to be in a maintenance state, as its last release was over eight years ago, suggesting infrequent updates but continued functionality. Its primary differentiation lies in its focused API for HTTP `Accept-Language` header processing, rather than a broader internationalization library.","status":"maintenance","version":"1.5.0","language":"javascript","source_language":"en","source_url":"git://github.com/opentable/accept-language-parser","tags":["javascript","accept-language","i18n","parser"],"install":[{"cmd":"npm install accept-language-parser","lang":"bash","label":"npm"},{"cmd":"yarn add accept-language-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add accept-language-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is primarily a CommonJS module. The 'require' syntax is the intended and most reliable way to import it in Node.js environments. In ESM, a default import pattern as shown in 'wrong' would likely work, but direct named imports will not.","wrong":"import parser from 'accept-language-parser';","symbol":"parser","correct":"const parser = require('accept-language-parser');"},{"note":"The `parse` function is a method on the default exported `parser` object. Direct named imports are not supported as it's a CommonJS module that doesn't provide named exports for ESM.","wrong":"import { parse } from 'accept-language-parser';","symbol":"parser.parse","correct":"const parser = require('accept-language-parser');\nconst languages = parser.parse('en-GB,en;q=0.8');"},{"note":"Similar to `parse`, `pick` is a method of the default exported `parser` object. Attempting to destructure it directly from the package will result in an error in ESM contexts.","wrong":"import { pick } from 'accept-language-parser';","symbol":"parser.pick","correct":"const parser = require('accept-language-parser');\nconst language = parser.pick(['fr-CA', 'fr-FR', 'fr'], 'en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8');"}],"quickstart":{"code":"const parser = require('accept-language-parser');\n\n// Example 1: Parsing an Accept-Language header\nconst acceptLanguageHeader = 'en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8';\nconsole.log(`\\n--- Parsing Header: ${acceptLanguageHeader} ---\\n`);\nconst parsedLanguages = parser.parse(acceptLanguageHeader);\nconsole.log('Parsed Languages (sorted by quality):', parsedLanguages);\n/* Expected Output:\n[\n  { code: 'en', region: 'GB', quality: 1 },\n  { code: 'en', region: 'US', quality: 0.9 },\n  { code: 'en', region: undefined, quality: 0.8 },\n  { code: 'fr', region: 'CA', quality: 0.7 }\n]\n*/\n\n// Example 2: Picking the best language from supported list\nconst supportedLanguages = ['fr-CA', 'fr-FR', 'fr', 'en-US', 'en'];\nconsole.log(`\\n--- Picking from Supported: [${supportedLanguages.join(', ')}] ---\\n`);\nconst bestMatch = parser.pick(supportedLanguages, acceptLanguageHeader);\nconsole.log('Best match (strict):', bestMatch);\n// Expected Output: fr-CA\n\n// Example 3: Picking with loose matching\nconst supportedLanguagesLoose = ['fr', 'en'];\nconsole.log(`\\n--- Picking with Loose Matching from Supported: [${supportedLanguagesLoose.join(', ')}] ---\\n`);\nconst bestMatchLoose = parser.pick(supportedLanguagesLoose, acceptLanguageHeader, { loose: true });\nconsole.log('Best match (loose):', bestMatchLoose);\n// Expected Output: fr\n","lang":"javascript","description":"Demonstrates parsing an `Accept-Language` header and picking the best matching language from a supported list, including an example of loose matching."},"warnings":[{"fix":"Ensure `supportedLanguagesArray` is ordered from most specific to least specific (e.g., `['fr-CA', 'fr']`) if prioritizing specific regional variants.","message":"When using the `loose` option in `parser.pick`, the order of the `supportedLanguagesArray` is crucial. The first partially matching language in the array will be returned, potentially leading to less specific matches if not ordered carefully.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"In ESM environments, use `import parser from 'accept-language-parser';` and then access methods like `parser.parse()` or `parser.pick()`.","message":"This package is predominantly a CommonJS module and does not natively support ES Module (ESM) named imports. Attempting `import { parse } from 'accept-language-parser'` will likely result in an error in ESM contexts.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Consider reviewing the source code for any potential issues if integrating into a critical system, or evaluate alternatives if active maintenance and modern features are a high priority.","message":"The package's last release (v1.5.0) was published over eight years ago. While it remains functional for its intended purpose, users should be aware of its inactive development status, meaning new features, bug fixes, or compatibility updates for newer Node.js versions or standards are unlikely.","severity":"gotcha","affected_versions":">=1.5.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Change the import to `import parser from 'accept-language-parser';` and access `parser.parse()` or `parser.pick()`.","cause":"Attempting to use `import { parse } from 'accept-language-parser';` in an ES Module context, while the package exports its functionality as properties of a default CommonJS object.","error":"TypeError: parser.parse is not a function"},{"fix":"Run `npm install accept-language-parser` or `yarn add accept-language-parser` in your project directory.","cause":"The package has not been installed, or the environment's module resolution paths are not correctly configured.","error":"Cannot find module 'accept-language-parser'"}],"ecosystem":"npm","meta_description":null}