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-regexpVerified import paths — ran on the pinned version, not inferred.
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.
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.
Ensure `browserslist` is installed as a dependency in your project: `npm install browserslist` or `pnpm add browserslist`.
Upgrade your Node.js environment to version 14 or higher.
Review your programmatic usage of the library and update any references from `regexp` to `regex` in option keys or properties.
Upgrade your Node.js environment to version 12 or higher.
Pass a `browsers` array to the `browserslistUseragentRegexp` function options or ensure a valid `.browserslistrc` file is in your project's root or configured location.
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.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.Install `browserslist` in your project: `npm install browserslist` or `pnpm add browserslist`.