bowser、detect-browser、ua-parser-js、useragent は、すべてユーザーエージェント文字列を解析してブラウザの種類、バージョン、OS、デバイスを特定するためのライブラリです。現代の Web 開発では機能検出(Feature Detection)が推奨されますが、分析ツールや特定のブラウザバグへの対処など、ユーザーエージェントの解析が必要になる場面仍然存在します。これらのライブラリは、クライアント側(ブラウザ)とサーバー側(Node.js)の両方で動作するものや、それぞれに特化したものがあり、提供される情報の詳細さやパフォーマンス、メンテナンス状況に違いがあります。
bowser、detect-browser、ua-parser-js、useragent は、ユーザーエージェント文字列を解析して環境を特定するための代表的なライブラリです。現代の Web 開発では、機能検出(Feature Detection)を優先すべきですが、アクセス解析や特定のブラウザバグへの対処など、ユーザーエージェントの解析が必要になる場面仍然存在します。これらのライブラリは、提供される情報の詳細さ、動作環境、メンテナンス状況に違いがあります。
ライブラリによって、 imports の方法や解析の呼び出し方が異なります。シンプルさ重視か、詳細な制御重視かで選び分ける必要があります。
bowser は、パーサーインスタンスを生成してから情報を取得するスタイルです。
import bowser from 'bowser';
const parser = bowser.getParser(window.navigator.userAgent);
const browser = parser.getBrowser();
console.log(browser.name); // "Chrome"
detect-browser は、関数を呼び出すだけで即座に情報を返す最もシンプルな API です。
import { detect } from 'detect-browser';
const browser = detect();
console.log(browser.name); // "chrome"
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"
useragent は、主に Node.js 環境を想定しており、文字列を直接渡して解析します。
import useragent from 'useragent';
const agent = useragent.parse(window.navigator.userAgent);
console.log(agent.family); // "Chrome"
プロジェクトによって、必要な情報の粒度は異なります。ブラウザ名だけで十分な場合もあれば、デバイス種類やレンダリングエンジンまで必要な場合もあります。
bowser は、ブラウザ、OS、プラットフォーム、エンジンをバランスよく提供します。
// bowser
const browser = parser.getBrowser();
const os = parser.getOS();
console.log(browser.version, os.name);
detect-browser は、ブラウザ名、バージョン、OS に絞った最小限の情報を提供します。
// detect-browser
console.log(browser.version, browser.os);
// デバイスやエンジンの詳細情報は含まれません
ua-parser-js は、ブラウザ、エンジン、OS、デバイス、CPU まで非常に詳細な情報を提供します。
// ua-parser-js
const result = parser.getResult();
console.log(result.engine.name, result.device.type);
useragent は、ブラウザファミリー、OS、デバイス情報を提供しますが、形式が他のライブラリとは異なります。
// useragent
console.log(agent.family, agent.os.family, agent.device.family);
universal なライブラリを選ぶか、環境に特化したライブラリを選ぶかも重要な判断基準です。
bowser はブラウザと Node.js の両方で動作しますが、主にクライアント側での利用を想定しています。
// bowser (Node.js)
const parser = bowser.getParser(req.headers['user-agent']);
detect-browser もブラウザと Node.js の両方をサポートしていますが、Node.js では環境変数などの扱いに注意が必要です。
// detect-browser (Node.js)
// 明示的に UA 文字列を渡す必要がある場合があります
const browser = detect(navigator.userAgent);
ua-parser-js は環境を選ばない設計で、サーバー側での解析にも強く推奨されます。
// ua-parser-js (Node.js)
const parser = new UAParser(req.headers['user-agent']);
useragent は歴史的に Node.js サーバー側での利用が主で、ブラウザ側での利用はあまり一般的ではありません。
// useragent (Node.js)
const agent = useragent.parse(req.headers['user-agent']);
ユーザーエージェント文字列は常に変化するため、ライブラリの更新頻度は重要です。
bowser は定期的に更新されており、新しいブラウザバージョンへの対応が比較的早いです。軽量さを保ちつつ信頼性があります。
detect-browser はシンプルであるがゆえに更新頻度は低めですが、基本的なブラウザ検出には十分です。複雑なケースには向かない可能性があります。
ua-parser-js はコミュニティでの採用数が多く、メンテナンスも活発です。信頼性が最も高い選択肢の一つです。
useragent は他のライブラリに比べて更新頻度が低い傾向があります。新規プロジェクトでは、よりモダンな代替案を検討することをお勧めします。
| 特徴 | bowser | detect-browser | ua-parser-js | useragent |
|---|---|---|---|---|
| API 形式 | インスタンス生成 | 関数呼び出し | クラスインスタンス | 関数呼び出し |
| 情報量 | 中〜高 | 低(基本のみ) | 高(詳細) | 中 |
| 環境 | ブラウザ主 | 両方 | 両方 | Node.js 主 |
| サイズ | 軽量 | 超軽量 | 標準 | 標準 |
| 保守性 | 良好 | 安定 | 非常に良好 | やや古い |
ua-parser-js は、詳細な情報が必要で、サーバー・クライアント両方での利用を想定する場合に最も信頼できる選択肢です。コミュニティでの支持も厚く、長期的なメンテナンスを考慮すると安心です。
bowser は、フロントエンドでのバンドルサイズを気にしつつ、十分な情報を得たい場合に最適です。現代の Web アプリケーション開発において、バランスの取れた選択肢と言えます。
detect-browser は、とにかく手軽にブラウザ名だけ知りたいというシンプルな要件に適しています。複雑な解析が必要ない場合に選びましょう。
useragent は、既存の Node.js プロジェクトで既に採用されている場合を除き、新規プロジェクトでは ua-parser-js や bowser の使用を検討する方が安全です。
どのライブラリを選ぶにしても、ユーザーエージェント解析は最終手段であることを忘れないでください。可能であれば、機能検出を活用する方が堅牢なアプリケーションになります。
クライアント側での軽量な解析が必要な場合に最適です。バンドルサイズを気にしつつ、ブラウザ名とバージョンを reliably に取得したいプロジェクトに向いています。サーバー側での利用も可能ですが、主にフロントエンドでの利用を想定して設計されています。
設定や複雑な操作なしに、すぐにブラウザ情報を取得したい場合に適しています。機能がシンプルなので、細かいデバイス情報やエンジン情報は不要で、ブラウザ名だけ分かれば良いというシンプルな要件に向いています。
ブラウザ、エンジン、OS、デバイスのすべてを詳細に解析したい場合に最適です。コミュニティでの採用実績が多く、メンテナンスも活発なので、長期的なプロジェクトやサーバー・クライアント両方で利用したい場合に信頼できます。
Node.js サーバー側でのレガシーな利用や、特定の正規表現ベースの解析が必要な場合に検討されます。ただし、他のモダンなライブラリに比べて更新頻度が低い傾向があるため、新規プロジェクトでは代替案の評価を推奨します。
A small, fast and rich-API browser/platform/engine detector for both browser and node.
Don't hesitate to support the project on Github or OpenCollective if you like it ❤️ Also, contributors are always welcome!
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 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.
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.
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"
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
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
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.
.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.
This project exists thanks to all the people who contribute. [Contribute].
Become a financial contributor and help us sustain our community. [Contribute]
Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]
Licensed as MIT. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.