Registry / testing / browserslist-useragent-regexp

browserslist-useragent-regexp

JSON →
library4.1.4jsnpmunverified

browserslist-useragent-regexp is a utility that compiles a `browserslist` query into a JavaScript regular expression, enabling client-side user agent detection. This allows developers to determine if a browser supports a given `browserslist` configuration directly in the browser, without server-side checks. It is currently at version 4.1.4 and receives active maintenance, with frequent bug fixes and minor feature releases. Key differentiators include its direct integration with `browserslist` for consistent browser targeting across toolchains and its ability to generate optimized regex patterns. Since version 4.0.0, it is an ESM-only package and requires Node.js >= 14. This library is particularly useful for conditional loading of polyfills or different JavaScript bundles based on client-side browser capabilities.

npm install browserslist-useragent-regexp
INSTALL
IMPORT
SIG · BROWSERSLIST-USERA
B
browserslist-useragent-regexp
testingjavascriptv4.1.4
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.

browserslistUseragentRegexp
import { browserslistUseragentRegexp } from 'browserslist-useragent-regexp';
const browserslistUseragentRegexp = require('browserslist-useragent-regexp');
The package is ESM-only since v4.0.0. The primary function to programmatically generate the regex is `browserslistUseragentRegexp`.
RegExp from generated file
import supportedBrowsersRegex from './supportedBrowsers.js';
A common usage pattern is to use the `browserslist-useragent-regexp` CLI to generate a JavaScript file (e.g., `supportedBrowsers.js`) that exports the compiled RegExp as a default export.
Type for function options
import type { Options } from 'browserslist-useragent-regexp';
TypeScript types are shipped with the package for its programmatic API, allowing for type-safe usage of the options object.

This quickstart demonstrates the common pattern of using the CLI to generate a JavaScript file containing the compiled RegExp, which is then imported and used to test a user agent string. It includes setup for the `.browserslistrc` and a `package.json` script, along with example runtime usage.

/* .browserslistrc */ // last 2 versions // not dead /* package.json */ // { // "scripts": { // "generate-regex": "echo \"export default $(browserslist-useragent-regexp --allowHigherVersions);\" > supportedBrowsers.js" // } // } // Run in your terminal once: // npm run generate-regex // --- supportedBrowsers.js (generated) --- // export default /Edge?\/(10[5-9]|1[1-9]\d|[2-9]\d\d|...)/; /* (actual regex will vary) */ // --- your-app.js --- import supportedBrowsersRegex from './supportedBrowsers.js'; function checkBrowserSupport() { const userAgent = typeof navigator !== 'undefined' ? navigator.userAgent : process.env.USER_AGENT ?? ''; if (userAgent === '') { console.warn('User-Agent string is empty. Cannot perform check.'); return; } if (supportedBrowsersRegex.test(userAgent)) { console.log(`User agent '${userAgent}' is supported.`); } else { console.warn(`User agent '${userAgent}' is NOT supported.`); } } // Example usage (replace with actual user agent for testing) process.env.USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'; checkBrowserSupport(); process.env.USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'; // IE 11 (likely unsupported) checkBrowserSupport();
Debug
Known issues
breakingThe package moved to ESM-only and dropped CommonJS support. Attempting to `require()` the package will result in an `ERR_REQUIRE_ESM` error.
fix
Migrate your import statements to use ES module syntax (e.g., `import ... from 'package';`). Ensure your project is configured for ESM, potentially by adding `"type": "module"` to `package.json` or using `.mjs` file extensions.
affects: >=4.0.0
breaking`browserslist` is no longer a direct dependency but a peer dependency. It must be installed separately in your project.
fix
Ensure `browserslist` is installed as a dependency in your project: `npm install browserslist` or `pnpm add browserslist`.
affects: >=4.0.0
breakingThe minimum required Node.js version was bumped to 14. Projects running on older Node.js versions will encounter compatibility issues.
fix
Upgrade your Node.js environment to version 14 or higher.
affects: >=4.0.0
breakingNaming conventions within the JavaScript API changed from `regexp` to `regex`. If you were programmatically using properties or options with `regexp` in their name, they are now `regex`.
fix
Review your programmatic usage of the library and update any references from `regexp` to `regex` in option keys or properties.
affects: >=4.0.0
breakingThe minimum required Node.js version was bumped to 12.
fix
Upgrade your Node.js environment to version 12 or higher.
affects: >=3.0.0 <4.0.0
gotchaWhen using the programmatic API (`browserslistUseragentRegexp`), you must either specify `browsers` in the options object or have a `.browserslistrc` file present in your project, otherwise, the `browserslist` query resolution will fail.
fix
Pass a `browsers` array to the `browserslistUseragentRegexp` function options or ensure a valid `.browserslistrc` file is in your project's root or configured location.
affects: >=2.0.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES module 'browserslist-useragent-regexp' from ... not supported.
Attempting to import an ESM-only package using CommonJS `require()` syntax.
fix
Change `const someVar = require('browserslist-useragent-regexp');` to `import { someVar } from 'browserslist-useragent-regexp';`. Ensure your project's `package.json` has `"type": "module"` or use `.mjs` file extensions for ESM files.
TypeError: The 'browsers' option is required.
The `browserslistUseragentRegexp` function was called without providing a `browsers` array in its options or without a resolvable `.browserslistrc` configuration.
fix
Provide a `browsers` array in the options object (e.g., `browserslistUseragentRegexp({ browsers: ['last 2 versions'] })`) or ensure a `.browserslistrc` file is correctly configured in your project.
Error: Cannot find package 'browserslist'. Did you mean to install it?
The `browserslist` package, a peer dependency since v4.0.0, is not installed in the project.
fix
Install `browserslist` in your project: `npm install browserslist` or `pnpm add browserslist`.
Upgrade
Version history
4.1.4latest on npm
Audit
Dependencies
browserslistrequiredRequired as a peer dependency since v4.0.0 to resolve browser queries.
Agent activity
17 hits · last 30 days
node
12
OpenAI (training)
2
Resources
browserslist-useragent-regexp — npm install browserslist-useragent-regexp · libregistry