UAParser.js: User-Agent & Client Hints Parser

2.0.9 · active · verified Tue Apr 21

UAParser.js is a comprehensive JavaScript library designed for detecting detailed information about a user's browser, operating system, CPU architecture, and device type/model. It leverages both traditional User-Agent strings and modern Client Hints data for accurate analysis. The current stable version is 2.0.9, with frequent patch releases indicating active development and continuous updates to its detection database for new browsers, OSes, and devices. This library is distinguished by its robust support for both client-side (browser) and server-side (Node.js) environments, offering a unified API. Key differentiators include its detailed detection capabilities, compact size, and up-to-date definitions, including specific submodules for bot and crawler detection, as well as features for chaining `withClientHints()` and `withFeatureCheck()`. A critical aspect for users is the change in licensing: while version 1.x was released under the permissive MIT License, version 2.x and onwards are distributed under the AGPL-3.0 License, which has significant implications for commercial and open-source projects.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates parsing a user-agent string, utilizing client hints for richer detection (relevant in server-side Node.js applications), and using a utility function from the 'extensions' submodule to check for bots. It highlights the primary API for both traditional and modern detection methods.

import UAParser from 'ua-parser-js';
import type { IResult } from 'ua-parser-js';

// Example 1: Parsing a standard User-Agent string
const userAgentString = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36';
const parser = new UAParser(userAgentString);
const result1: IResult = parser.getResult();
console.log('Result from User-Agent:', JSON.stringify(result1, null, 2));

// Example 2: Parsing with Client Hints (Node.js environment usually)
// In a real application, these headers would come from an incoming HTTP request.
const clientHintsHeaders = {
  'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
  'sec-ch-ua-mobile': '?0',
  'sec-ch-ua-platform': '"macOS"',
  'sec-ch-ua-platform-version': '"14.4.1"',
  'sec-ch-ua-model': '',
  'sec-ch-ua-arch': '"arm"'
};

// Note: Headers must be an instance of `Headers` or compatible object for `withClientHints`.
// In Node.js, this might involve converting `http.IncomingHttpHeaders`.
const headersInstance = new Headers(clientHintsHeaders as any); // Type assertion for demo simplicity

const parserWithClientHints = new UAParser(userAgentString)
  .withClientHints(headersInstance)
  .getResult();

console.log('\nResult from Client Hints:', JSON.stringify(parserWithClientHints, null, 2));

// Example 3: Using a submodule like bot-detection
import { isBot } from 'ua-parser-js/extensions/bot-detection';

const botUserAgent = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
const isGoogleBot = isBot(botUserAgent);
console.log(`\nIs '${botUserAgent}' a bot? ${isGoogleBot}`);

view raw JSON →