Browserslist Useragent Regexp
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.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES module 'browserslist-useragent-regexp' from ... not supported.
cause Attempting to import an ESM-only package using CommonJS `require()` syntax.fixChange `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.
cause The `browserslistUseragentRegexp` function was called without providing a `browsers` array in its options or without a resolvable `.browserslistrc` configuration.fixProvide 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?
cause The `browserslist` package, a peer dependency since v4.0.0, is not installed in the project.fixInstall `browserslist` in your project: `npm install browserslist` or `pnpm add browserslist`.
Warnings
- breaking The package moved to ESM-only and dropped CommonJS support. Attempting to `require()` the package will result in an `ERR_REQUIRE_ESM` error.
- breaking `browserslist` is no longer a direct dependency but a peer dependency. It must be installed separately in your project.
- breaking The minimum required Node.js version was bumped to 14. Projects running on older Node.js versions will encounter compatibility issues.
- breaking Naming 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`.
- breaking The minimum required Node.js version was bumped to 12.
- gotcha When 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.
Install
-
npm install browserslist-useragent-regexp -
yarn add browserslist-useragent-regexp -
pnpm add browserslist-useragent-regexp
Imports
- browserslistUseragentRegexp
const browserslistUseragentRegexp = require('browserslist-useragent-regexp');import { browserslistUseragentRegexp } from 'browserslist-useragent-regexp'; - RegExp from generated file
import supportedBrowsersRegex from './supportedBrowsers.js';
- Type for function options
import type { Options } from 'browserslist-useragent-regexp';
Quickstart
/* .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();