mobile-detect.js

A JavaScript port of the PHP Mobile-Detect library that identifies phones, tablets, OS, and browser versions by matching a user-agent string against an extensive device-pattern ruleset.

Library
npm
v1.4.5
4,137stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
45/100Fair
Development Activity0
Maintenance0
Community80
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
55/100Fair
Architecture58
Code Quality55
Innovation40
Learning Curve65

mobile-detect.js is a loose JavaScript port of the popular PHP library Mobile-Detect. Given a user-agent string, it tells you whether the request came from a phone or tablet, which vendor/model family it belongs to, which operating system and browser are in play, and lets you pull out specific version numbers (WebKit, Android, iOS, Build, and dozens more). It ships as a single UMD file with zero runtime dependencies, so it drops straight into a <script> tag in the browser or an npm install in a Node.js/Express backend.

Under the hood, the library is really a very large, continuously updated set of regular expressions covering phones, tablets, operating systems, and user-agent strings, generated from the upstream Mobile-Detect PHP project’s rule configuration. The public API (mobile(), phone(), tablet(), os(), is(), match(), version(), mobileGrade()) is a thin, well-documented (JSDoc) layer over those pattern tables, with results cached per-instance so repeated calls on the same user agent are cheap. The README itself is candid that user-agent sniffing is inherently fragile compared to feature detection or media queries, and recommends encapsulating usage rather than spreading MobileDetect calls throughout an application.

What You Get

  • A single dependency-free UMD module that works via <script> tag, CommonJS require, or AMD define
  • Dedicated phone(), tablet(), and mobile() accessors that return the matched vendor/family name or null
  • os() and version()/versionStr() methods for pulling operating system and specific component versions (WebKit, Build, Android, iOS, etc.) out of the user-agent
  • A generic is(key) check and raw match(pattern) regex escape hatch for anything the built-in accessors don’t cover
  • A mobileGrade() heuristic (A/B/C) approximating the old jQuery Mobile browser-support grading
  • Bundled TypeScript type definitions (mobile-detect.d.ts) for typed projects

Common Use Cases

  • Server-side rendering a lighter mobile layout or redirecting to a mobile subdomain based on the incoming User-Agent header in an Express/Node.js app
  • Client-side feature or layout branching in legacy browser-only codebases that predate reliable CSS media queries
  • Tagging analytics events with device/OS/browser family for reporting
  • Quick device-family checks in QA or debugging tools that need to interpret raw user-agent strings
  • Generating mobile-usage statistics via the companion hgoebl/mobile-usage project

Under The Hood

Architecture The library is a single generated file (mobile-detect.js) built from generate/mobile-detect.template.js plus a rules payload pulled from the upstream PHP serbanghita/Mobile-Detect project via generate/export-config.php and generate/generate.js (using the mote templating engine). At runtime it boils down to one large impl.mobileDetectRules object (phones, tablets, oss, uas, props, utils regex tables) and a MobileDetect constructor whose accessor methods (mobile, phone, tablet, os, userAgent, mobileGrade) lazily populate a per-instance _cache via shared findMatch/findMatches helpers. There is no dependency injection or layering beyond data-vs-accessors; every public method ultimately depends on the same rule tables, so a change to impl.mobileDetectRules ripples through all detection methods at once.

Tech Stack Plain ES5 JavaScript with zero runtime dependencies (package.json declares an empty dependencies object), distributed as a UMD module supporting CommonJS (module.exports), AMD (define), and a window.MobileDetect browser global. The dev toolchain is Grunt-based (grunt-contrib-concat, grunt-contrib-jshint, grunt-contrib-uglify, grunt-jasmine-node, grunt-jsdoc), and regenerating the ruleset requires a local PHP interpreter to run export-config.php against a sibling clone of the PHP Mobile-Detect repo. TypeScript consumers get hand-authored typings via mobile-detect.d.ts.

Code Quality Tests live in tests/spec/MobileDetectSpec.js (Jasmine, run via grunt jasmine_node or in-browser via tests/SpecRunner.html), backed by fixture data in tests/data. JSHint (.jshintrc) enforces baseline style rules (curly braces, eqeqeq, latedef, undef), but there is no CI workflow configured in .github/ (only an issue template) — tests only run when a maintainer or contributor executes Grunt locally. Comment density is high: nearly every public method carries a detailed JSDoc block.

What Makes It Unique Its distinguishing trait is being a faithful, continuously-regenerated JavaScript mirror of the well-established PHP Mobile-Detect ruleset rather than an independently maintained detection engine — the actual device-matching logic is authored upstream in PHP and mechanically transpiled into this repo. Beyond that sourcing model, the detection approach (regex tables keyed by vendor) is standard for this category of library, and the README itself is unusually candid that user-agent sniffing is a fragile, legacy technique that newer projects should generally avoid in favor of feature detection or media queries.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search