bowser vs platform vs ua-parser-js
ユーザーエージェント解析と環境検出ライブラリの比較
bowserplatformua-parser-js類似パッケージ:

ユーザーエージェント解析と環境検出ライブラリの比較

bowserplatformua-parser-js は、JavaScript 環境においてユーザーエージェント文字列を解析し、ブラウザ、OS、デバイスの情報を取得するためのライブラリです。bowser はブラウザ特定のロジックに強く、ua-parser-js は詳細なデバイス情報と汎用性に優れています。platform は主に現在の実行環境を検出する轻量なツールですが、メンテナンス頻度は他より低めです。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
bowser05,737258 kB914ヶ月前MIT
platform03,246-346年前MIT
ua-parser-js010,1271.32 MB196日前AGPL-3.0-or-later

bowser vs platform vs ua-parser-js: 環境検出と UA 解析のアーキテクチャ比較

Web 開発において、ユーザーがどのような環境でアプリケーションを実行しているかを把握することは、互換性の確保や分析のために重要です。bowserplatformua-parser-js はこの課題を解決する代表的なライブラリですが、設計思想と得意分野が異なります。ここでは、実務的な観点からこれらを比較します。

🛠️ 基本的な使い方と API 設計

各ライブラリは、情報へのアクセス方法が異なります。bowserua-parser-js はパーサーインスタンスを作成するスタイルですが、platform は環境情報を保持するオブジェクトを直接エクスポートします。

bowser は、ユーザーエージェント文字列を渡してパーサーを作成し、メソッドで情報を取得します。

// bowser: パーサーインスタンスを作成
import bowser from 'bowser';

const parser = bowser.getParser(window.navigator.userAgent);
const browserName = parser.getBrowserName();

console.log(browserName); // "Chrome"

ua-parser-js も同様にインスタンス化しますが、より構造化されたデータを返す傾向があります。

// ua-parser-js: インスタンス化して結果を取得
import UAParser from 'ua-parser-js';

const parser = new UAParser(window.navigator.userAgent);
const result = parser.getResult();

console.log(result.browser.name); // "Chrome"

platform は、インスタンス化せず、エクスポートされたオブジェクトから直接プロパティにアクセスします。主に現在の環境を検出します。

// platform: グローバルオブジェクトから直接アクセス
import platform from 'platform';

console.log(platform.name); // "Chrome"
console.log(platform.version); // "114.0"

📊 取得できる情報の詳細度

プロジェクトで必要な情報の粒度によって、適切なツールが変わります。単純なブラウザ名だけで十分か、デバイスのモデル名まで必要かを確認しましょう。

bowser は、ブラウザ名、バージョン、およびプラットフォーム(モバイル/タブレット/デスクトップ)の判定に特化しています。

// bowser: ブラウザとプラットフォームの判定
const browserName = parser.getBrowserName();
const browserVersion = parser.getBrowserVersion();
const platformType = parser.getPlatformType(); // "mobile", "tablet", "desktop"

ua-parser-js は、ブラウザ、OS、デバイス、エンジン、CPU まで詳細に解析します。デバイスモデル名などが取得できる点が強みです。

// ua-parser-js: 詳細なデバイスと OS 情報
const device = parser.getDevice();
const os = parser.getOS();

console.log(device.model); // "iPhone"
console.log(os.name); // "iOS"

platform は、基本的なブラウザ名、バージョン、OS、製品情報を提供しますが、デバイスモデルなどの詳細情報は限定的です。

// platform: 基本的な環境情報
console.log(platform.os.family); // "OS X"
console.log(platform.product); // "iPhone"
// デバイスモデルなどの詳細は取得できない場合が多い

🌐 任意のユーザーエージェント文字列の解析

サーバーサイドレンダリング(SSR)やログ分析では、現在の環境ではなく、クライアントから送信された任意の UA 文字列を解析する必要があります。

bowser は、任意の文字列を渡して解析するのが主な用途です。サーバー側での利用に適しています。

// bowser: 任意の UA 文字列を解析
const uaString = "Mozilla/5.0 (iPhone; CPU iPhone OS...)";
const parser = bowser.getParser(uaString);
const isMobile = parser.isMobile();

ua-parser-js も任意の文字列をコンストラクタに渡すことで、同じように解析できます。ログ分析ツールなどでよく使われます。

// ua-parser-js: 任意の UA 文字列を解析
const uaString = "Mozilla/5.0 (iPhone; CPU iPhone OS...)";
const parser = new UAParser(uaString);
const deviceType = parser.getDevice().type;

platform は、基本的に現在の実行環境を検出するために設計されています。任意の文字列を解析する機能は標準的な使い方ではなく、この用途には適していません。

// platform: 現在の環境のみ検出(任意の文字列解析は非推奨)
// 現在の navigator.userAgent を自動的に読み取ります
console.log(platform.name);
// 外部から受け取った UA 文字列の解析には使わないでください

🧩 ブラウザ特定のロジックと判定

特定のブラウザバージョンに基づいて処理を分ける場合、API の使いやすさが開発効率に影響します。

bowser は、ブラウザ名とバージョン範囲を指定して判定する機能が充実しています。

// bowser: バージョン範囲での判定
const isValid = parser.satisfies({
  chrome: ">60",
  safari: ">10"
});

if (isValid) {
  // 処理を実行
}

ua-parser-js はデータを取得するだけで、判定ロジックは開発者が実装する必要があります。柔軟性は高いですが、コード量が増えます。

// ua-parser-js: 手動でバージョン判定
const browser = parser.getBrowser();
const version = parseFloat(browser.version);

if (browser.name === "Chrome" && version > 60) {
  // 処理を実行
}

platform は、環境情報を提供するだけで、判定ヘルパーは提供していません。すべて手動実装が必要です。

// platform: 手動で条件分岐
if (platform.name === "Chrome" && parseFloat(platform.version) > 60) {
  // 処理を実行
}

⚠️ メンテナンスと将来性

ライブラリの選択では、将来にわたってメンテナンスされるかも重要な基準です。ユーザーエージェントは常に新しいデバイスやブラウザで更新されるため、ライブラリの更新頻度は重要です。

bowser は定期的に更新されており、新しいブラウザのバージョンに対応しています。コミュニティによるメンテナンスが活発です。

// bowser: 定期的に更新される定義ファイル
// 最新の Safari や Chrome のバージョンもスムーズに検出可能

ua-parser-js は、この分野で最も広く使われており、新しいデバイス情報への対応が最も早いです。コミュニティによる UA 文字列の定義更新が頻繁に行われます。

// ua-parser-js: 最も豊富なデバイス定義
// 新規リリースされたスマートフォンなども早期にサポートされる

platform は、更新頻度が他と比較して低く、最新のモバイルデバイス情報などが欠けている可能性があります。新しいプロジェクトでの採用は慎重に検討すべきです。

// platform: 更新が停滞している可能性
// 最新の iPad や Android デバイスが正しく検出されないリスクがある

📌 比較サマリー

特徴bowserplatformua-parser-js
主な用途ブラウザ判定ロジック現在の環境検出詳細な UA 解析
任意 UA 解析✅ 対応❌ 非推奨✅ 対応
デバイス情報種類(モバイル等)限定的詳細(モデル名等)
判定ヘルパー✅ 充実 (satisfies)❌ 手動❌ 手動
メンテナンス活発低調非常に活発

💡 結論と推奨

bowser は、フロントエンドコード内で「このブラウザならこの処理」といった条件分岐を書きたい場合に最適です。API がシンプルで、バージョン判定が容易な点が魅力です。

ua-parser-js は、分析ダッシュボードや、デバイス種別を厳密に判別が必要な場合に選択すべきです。得られる情報の豊富さと、コミュニティによる更新頻度が信頼性を支えています。

platform は、軽量な環境検出が必要な場合や、既存資産として使われている場合に限り検討します。新しいプロジェクトで詳細なデバイス検出が必要な場合は、ua-parser-js の方が安全な選択です。

最終的には、「現在の環境検出だけで良いか」「詳細なデバイス情報が必要か」「ブラウザ判定ロジックを簡略化したいか」 という 3 点で要件を整理すると、適切なライブラリが見えてきます。

選び方: bowser vs platform vs ua-parser-js

  • bowser:

    bowser は、ブラウザの種類に基づいて条件分岐させたい場合に最適です。例えば、特定のブラウザバージョンのみで CSS クラスを適用する際や、既知のブラウザバグを回避するロジックに適しています。API が直感的で、ブラウザ名やバージョンの取得が簡単です。

  • platform:

    platform は、現在の実行環境(Node.js かブラウザか)を軽量に検出したい場合に適しています。ただし、更新頻度が低く、最新のモバイルデバイス情報の取得には向いていません。既存プロジェクトで既に使われている場合を除き、新しいプロジェクトでは他の選択肢を検討すべきです。

  • ua-parser-js:

    ua-parser-js は、ユーザーエージェント文字列から最大限の詳細な情報を引き出したい場合に選択します。.analytics ダッシュボードや、デバイス種別(タブレット vs スマホ)の厳密な判定が必要な場合に最適です。コミュニティによる UA 文字列の更新が頻繁に行われています。

bowser のREADME

Bowser

A small, fast and rich-API browser/platform/engine detector for both browser and node.

  • Small. Use plain ES5-version which is ~4.8kB gzipped.
  • Optimized. Use only those parsers you need — it doesn't do useless work.
  • Multi-platform. It's browser- and node-ready, so you can use it in any environment.

Don't hesitate to support the project on Github or OpenCollective if you like it ❤️ Also, contributors are always welcome!

Financial Contributors on Open Collective Downloads

Contents

Overview

The library is made to help to detect what browser your user has and gives you a convenient API to filter the users somehow depending on their browsers. Check it out on this page: https://bowser-js.github.io/bowser-online/.

⚠️ Version 2.0 breaking changes ⚠️

Version 2.0 has drastically changed the API. All available methods are on the docs page.

For legacy code, check out the 1.x branch and install it through npm install bowser@1.9.4.

Use cases

First of all, require the library. This is a UMD Module, so it will work for AMD, TypeScript, ES6, and CommonJS module systems.

const Bowser = require("bowser"); // CommonJS

import * as Bowser from "bowser"; // TypeScript

import Bowser from "bowser"; // ES6 (and TypeScript with --esModuleInterop enabled)

By default, the exported version is the ES5 transpiled version, which do not include any polyfills.

In case you don't use your own babel-polyfill you may need to have pre-built bundle with all needed polyfills. So, for you it's suitable to require bowser like this: require('bowser/bundled'). As the result, you get a ES5 version of bowser with babel-polyfill bundled together.

You may need to use the source files, so they will be available in the package as well.

Browser props detection

Often we need to pick users' browser properties such as the name, the version, the rendering engine and so on. Here is an example how to do it with Bowser:

const browser = Bowser.getParser(window.navigator.userAgent);

console.log(`The current browser name is "${browser.getBrowserName()}"`);
// The current browser name is "Internet Explorer"

Using User-Agent Client Hints

Modern browsers support User-Agent Client Hints, which provide a more privacy-friendly and structured way to access browser information. Bowser can use Client Hints data to improve browser detection accuracy.

// Pass Client Hints as the second parameter
const browser = Bowser.getParser(
  window.navigator.userAgent,
  window.navigator.userAgentData
);

console.log(`The current browser name is "${browser.getBrowserName()}"`);
// More accurate detection using Client Hints

Working with Client Hints

Bowser provides methods to access and query Client Hints data:

const browser = Bowser.getParser(
  window.navigator.userAgent,
  window.navigator.userAgentData
);

// Get the full Client Hints object
const hints = browser.getHints();
// Returns the ClientHints object or null if not provided

// Check if a specific brand exists
if (browser.hasBrand('Google Chrome')) {
  console.log('This is Chrome!');
}

// Get the version of a specific brand
const chromeVersion = browser.getBrandVersion('Google Chrome');
console.log(`Chrome version: ${chromeVersion}`);

The Client Hints object structure:

{
  brands: [
    { brand: 'Google Chrome', version: '131' },
    { brand: 'Chromium', version: '131' },
    { brand: 'Not_A Brand', version: '24' }
  ],
  mobile: false,
  platform: 'Windows',
  platformVersion: '15.0.0',
  architecture: 'x86',
  model: '',
  wow64: false
}

Note: Client Hints improve detection for browsers like DuckDuckGo and other Chromium-based browsers that may have similar User-Agent strings. When Client Hints are not provided, Bowser falls back to standard User-Agent string parsing.

or

const browser = Bowser.getParser(window.navigator.userAgent);
console.log(browser.getBrowser());

// outputs
{
  name: "Internet Explorer"
  version: "11.0"
}

or

console.log(Bowser.parse(window.navigator.userAgent));

// outputs
{
  browser: {
    name: "Internet Explorer"
    version: "11.0"
  },
  os: {
    name: "Windows"
    version: "NT 6.3"
    versionName: "8.1"
  },
  platform: {
    type: "desktop"
  },
  engine: {
    name: "Trident"
    version: "7.0"
  }
}

You can also use Bowser.parse() with Client Hints:

console.log(Bowser.parse(window.navigator.userAgent, window.navigator.userAgentData));

// Same output structure, but with enhanced detection from Client Hints

Filtering browsers

You could want to filter some particular browsers to provide any special support for them or make any workarounds. It could look like this:

const browser = Bowser.getParser(window.navigator.userAgent);
const isValidBrowser = browser.satisfies({
  // declare browsers per OS
  windows: {
    "internet explorer": ">10",
  },
  macos: {
    safari: ">10.1"
  },

  // per platform (mobile, desktop or tablet)
  mobile: {
    safari: '>=9',
    'android browser': '>3.10'
  },

  // or in general
  chrome: "~20.1.1432",
  firefox: ">31",
  opera: ">=22",

  // also supports equality operator
  chrome: "=20.1.1432", // will match particular build only

  // and loose-equality operator
  chrome: "~20",        // will match any 20.* sub-version
  chrome: "~20.1"       // will match any 20.1.* sub-version (20.1.19 as well as 20.1.12.42-alpha.1)
});

Settings for any particular OS or platform has more priority and redefines settings of standalone browsers. Thus, you can define OS or platform specific rules and they will have more priority in the end.

More of API and possibilities you will find in the docs folder.

Browser names for .satisfies()

By default you are supposed to use the full browser name for .satisfies. But, there's a short way to define a browser using short aliases. The full list of aliases can be found in the file.

Similar Projects

  • Kong - A C# port of Bowser.

Contributors

Code Contributors

This project exists thanks to all the people who contribute. [Contribute].

Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

License

Licensed as MIT. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.