Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

browserslist-useragent

pastelsky353.9kMIT4.0.0TypeScript support: definitely-typed

A utility to match a browselist query to browser user agents

readme

Browserslist Useragent

build npm

Browserslist Useragent logo (original by Anton Lovchikov)

Find if a given user agent string satisfies a browserslist query.

It automatically reads the browserslist configuration specified in your project, but you can also specify the same using the options parameter.

If you wish to target modern browsers, read this.

Installation

Note, browserslist is a peer dependency, so make sure you have that installed in your project.

npm install browserslist-useragent
# or 
yarn add browserslist-useragent

Usage

const { matchesUA } = require('browserslist-useragent')

matchesUA(userAgentString, options)

// with browserslist config inferred
matchesUA('Mozilla/5.0 (Windows NT 10.0; rv:54.0) Gecko/20100101 Firefox/54.0')
//returns boolean

// with explicit browserslist
matchesUA(
  'Mozilla/5.0 (Windows NT 10.0; rv:54.0) Gecko/20100101 Firefox/54.0',
  { browsers: ['Firefox > 53'] }
)
// returns true
Option Default Value Description
browsers Manually provide a browserslist query (or an array of queries). Specifying this overrides the browserslist configuration specified in your project.
env When multiple browserslist environments are specified, pick the config belonging to this environment.
path process.cwd() Specify a folder to search for the browserslist config (if it differs from the current working directory)
ignorePatch true Ignore differences in patch browser numbers
ignoreMinor false Ignore differences in minor browser versions
allowHigherVersions false For all the browsers in the browserslist query, return a match if the user agent version is equal to or higher than the one specified in browserslist. See why this might be useful.

Supported browsers

  • Chrome (Chrome / Chromium / Yandex) as and_chr | ChromeAndroid | Chrome
  • Samsung Internet as Samsung
  • Firefox as ff | and_ff | FirefoxAndroid | Firefox
  • Safari iOS as ios_saf | iOS
  • Safari Desktop as Safari
  • IE as ie | ie_mob
  • Edge as Edge
  • Electron as Electron

PRs to add more browserslist supported browsers are welcome 👋

Notes

  • All browsers on iOS (Chrome, Firefox etc) use Safari's WebKit as the underlying engine, and hence will be resolved to Safari. Since browserslist is usually used for transpiling / autoprefixing for browsers, this behaviour is what's intended in most cases, but might surprise you otherwise.

  • Right now, Chrome for Android and Firefox for Android are resolved to their desktop equivalents. The caniuse database does not currently store historical data for these browsers separately (except the last version) See #156. However, safari for iOS and desktop can be matched separately, since this data is available for both.

When querying for modern browsers

  • It is a good idea to update this package often so that browser definitions are upto date.
  • It is also a good idea to add unreleased versions to your browserslist query, and set ignoreMinor and ignorePatch to true so that alpha / beta / canary versions of browsers are matched.
  • In case you're unable to keep this package up-to-date, you can set the allowHigherVersions to true. For all the browsers specified in your browserslist query, this will return a match if the user agent version is equal to or higher than those specified in your browserslist query. Use this with care though, since it's a wildcard, and only lightly tested.

Further reads

changelog

Please use github releases page to see changes for future versions.

v3.0.0

  • Fix: Don't crash when options parameter is not specified
  • Fix: Use loose option to parse browsers versions. Doesn't crash on versions like 6.00.1 or 6.0.0005` etc.

  • Feature: Added support for Samsung Internet

  • Breaking: Safari on desktop and on iOS are now considered as separate browsers. You'll need to use separate browserslist queries to target both of them - ['Safari >= 10', 'iOS >= 10.1']. See #23

  • Breaking: Removing support for undocumented option to matchesUA - all. This was used internally, and shouldn't be breaking change for you unless you poked around code to discover it.
  • Breaking: Remove deprecated _allowHigherVersions flag. This has been replaced by allowHigherVersions in the past.

v2.0.1

  • Remove deprecated _allowHigherVersions option from README

v2.0.0

  • Feature: Added Yandex to list of supported browsers
  • Feature: Added support for Yandex Browser
  • Deprecation: Deprecated _allowHigherVersions

v1.2.1

  • Fixed build link

v1.2.0

  • Feature: Updated packages and added support for headless chrome and chromium
  • Fixed links in badges

v1.1.0

  • Feature: Added support for selecting environment
  • Updated browserslist version

v1.0.4

  • delete gitignore

v1.0.3

  • Update README.md

v1.0.2

  • Second try at fixing logo

v1.0.1

  • Updated browserslist version

v1.0.0

  • Create LICENSE
  • /browserlist/ => browserslist
  • Refactored code and rewrote Readme

v0.0.2

  • Added install instructions
  • Initial commit