express-device vs node-device-detector vs ua-parser-js
Device and User-Agent Detection in Node.js Applications
express-devicenode-device-detectorua-parser-jsSimilar Packages:

Device and User-Agent Detection in Node.js Applications

express-device, node-device-detector, and ua-parser-js are all npm packages designed to parse HTTP User-Agent strings to identify client devices, browsers, and operating systems. These tools help developers tailor responses or experiences based on the detected client characteristics. ua-parser-js is a general-purpose, framework-agnostic library focused purely on parsing. express-device is an Express.js middleware that automatically attaches device info to the request object. node-device-detector offers more detailed device classification with support for modern detection techniques beyond basic User-Agent parsing.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
express-device35,379305-611 years agoMIT
node-device-detector01834.06 MB55 months agoMIT
ua-parser-js010,1051.31 MB163 months agoAGPL-3.0-or-later

Device Detection in Node.js: express-device vs node-device-detector vs ua-parser-js

Parsing User-Agent strings to detect devices, browsers, and operating systems is a common need in web applications—for analytics, responsive rendering, or compatibility checks. But not all detection libraries are created equal. Let’s compare express-device, node-device-detector, and ua-parser-js from a practical engineering standpoint.

🧩 Core Philosophy: Middleware vs Parser vs Detector

express-device is Express-specific middleware. It hooks into your Express app and automatically adds a req.device property so you can check device type inside route handlers.

// express-device: automatic injection into req
const express = require('express');
const device = require('express-device');

const app = express();
app.use(device.capture());

app.get('/', (req, res) => {
  // req.device.type is 'phone', 'tablet', or 'desktop'
  res.send(`You're on a ${req.device.type}`);
});

ua-parser-js is a pure parser with no framework dependencies. You pass it a User-Agent string and get back structured data about browser, OS, and device.

// ua-parser-js: manual parsing, works anywhere
const UAParser = require('ua-parser-js');

const ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) ...';
const parser = new UAParser(ua);

const result = parser.getResult();
// { browser: { name: 'Mobile Safari', version: '16.5' },
//   os: { name: 'iOS', version: '16.5' },
//   device: { vendor: 'Apple', model: 'iPhone', type: 'mobile' } }

node-device-detector is a full-featured detector that goes beyond basic parsing. It uses an extensive internal database and additional heuristics to identify exact device models.

// node-device-detector: detailed device identification
const DeviceDetector = require('node-device-detector');
const detector = new DeviceDetector();

const ua = 'Mozilla/5.0 (Linux; Android 13; SM-S908B) ...';
const result = detector.detect(ua);
// { os: { name: 'Android', version: '13' },
//   client: { type: 'browser', name: 'Chrome' },
//   device: { type: 'smartphone', brand: 'Samsung', model: 'Galaxy S22+' } }

📱 Accuracy and Detail Level

The level of detail varies significantly:

  • express-device only tells you if the device is a phone, tablet, or desktop. It doesn’t identify brands, models, or even distinguish between iOS and Android.
// express-device output example
req.device.type; // 'phone' — that’s it
  • ua-parser-js provides vendor, model, and device type, but its device detection is limited to what’s inferable from the User-Agent alone. Many Android devices appear as generic "Generic Smartphone".
// ua-parser-js may return:
result.device; // { vendor: 'Samsung', model: 'SM-G975F', type: 'mobile' }
// But often:
result.device; // { vendor: undefined, model: undefined, type: 'mobile' }
  • node-device-detector maintains a large, frequently updated regex-based device database (similar to what commercial services use). It reliably identifies specific models like iPhone 14 Pro or Xiaomi Redmi Note 12.
// node-device-detector output
result.device; // { type: 'smartphone', brand: 'Apple', model: 'iPhone 14 Pro' }

If your use case depends on knowing whether a user is on an iPhone 12 vs iPhone 13, only node-device-detector delivers consistently.

⚙️ Integration and Flexibility

express-device is tightly coupled to Express. You can’t use it in Koa, Fastify, or frontend code. It also runs on every request, which may be unnecessary overhead if you only need device info on certain routes.

ua-parser-js works everywhere — browser, Node.js, Deno, or even React Native. You decide when and where to parse, and you can cache parsers for performance.

// Reuse parser instance for better performance
const parser = new UAParser();
function handleRequest(ua) {
  parser.setUA(ua);
  return parser.getDevice();
}

node-device-detector is Node.js-only (due to its large internal database), but it’s framework-agnostic. You can use it in any server environment by manually passing the User-Agent header.

// Works in any Node.js HTTP server
app.get('/', (req, res) => {
  const device = detector.detect(req.headers['user-agent']);
  // ...
});

🛠️ Maintenance and Reliability

As of 2024, express-device shows signs of abandonment: its last meaningful update was years ago, and it relies on outdated detection logic. New devices often get misclassified or fall back to generic categories.

In contrast, both ua-parser-js and node-device-detector are actively maintained. ua-parser-js syncs regularly with the official UAParser community regexes. node-device-detector updates its internal database frequently and supports modern detection patterns beyond raw User-Agent parsing (like client hints, though that requires additional setup).

⚠️ Important: Because express-device is effectively unmaintained and lacks accuracy, it should not be used in new projects. Even for simple Express apps, calling ua-parser-js manually inside middleware gives you better results with minimal extra code.

🌐 Real-World Usage Scenarios

Scenario 1: Basic Mobile Redirect in Express

You want to redirect mobile users to /m.

  • ❌ Avoid express-device — too unreliable.
  • ✅ Use ua-parser-js with custom middleware:
const UAParser = require('ua-parser-js');

app.use((req, res, next) => {
  const parser = new UAParser(req.headers['user-agent']);
  const deviceType = parser.getDevice().type;
  if (deviceType === 'mobile' && !req.url.startsWith('/m')) {
    return res.redirect('/m' + req.url);
  }
  next();
});

Scenario 2: Analytics Dashboard Showing Device Models

You’re building an internal tool that displays which phone models your users have.

  • Best choice: node-device-detector
  • Why? Only it provides consistent, model-level accuracy needed for meaningful metrics.
const detector = new DeviceDetector();
const { device } = detector.detect(userAgent);
// Log: { brand: 'Google', model: 'Pixel 7' }

Scenario 3: Feature Detection for Legacy Browser Support

You need to warn users on old browsers.

  • Best choice: ua-parser-js
  • Why? It gives precise browser name and version, works in the browser too (for client-side fallbacks), and is lightweight.
const parser = new UAParser(navigator.userAgent);
const browser = parser.getBrowser();
if (browser.name === 'IE' || (browser.name === 'Chrome' && parseFloat(browser.version) < 80)) {
  showUpgradeWarning();
}

📊 Summary Table

Featureexpress-devicenode-device-detectorua-parser-js
Framework CouplingExpress-onlyNone (Node.js only)None (universal)
Device Detail LevelType only (3 categories)Full model + brand + typeVendor/model if available
AccuracyLowHighMedium
Maintenance StatusUnmaintainedActively maintainedActively maintained
Use in Frontend?NoNoYes
Good ForLegacy Express appsAnalytics, ad tech, precise UXGeneral parsing, feature detection

💡 Final Recommendation

  • Avoid express-device in new code. Its convenience isn’t worth the technical debt.
  • Use ua-parser-js for most general-purpose User-Agent parsing — it’s reliable, lightweight, and works everywhere.
  • Reach for node-device-detector only when you truly need high-fidelity device identification and are operating in a Node.js backend context.

Remember: User-Agent parsing is inherently imperfect. For critical functionality, always combine it with progressive enhancement and feature detection (e.g., using @mdn/browser-compat-data or runtime checks) rather than relying solely on device labels.

How to Choose: express-device vs node-device-detector vs ua-parser-js

  • express-device:

    Choose express-device only if you're working within an Express.js application and need a quick, zero-config way to attach basic device type (e.g., desktop, mobile, tablet) to each incoming request. It’s convenient but limited in accuracy and detail, and hasn’t seen active maintenance in recent years — avoid it for new projects requiring reliable or granular device detection.

  • node-device-detector:

    Choose node-device-detector when you need high-accuracy device identification—including specific models like 'iPhone 14' or 'Samsung Galaxy S23'—and are willing to accept a slightly heavier dependency. It combines User-Agent parsing with additional heuristics and maintains an up-to-date device database, making it suitable for analytics, ad targeting, or responsive rendering decisions that depend on precise hardware info.

  • ua-parser-js:

    Choose ua-parser-js when you need a lightweight, battle-tested, and actively maintained library that parses User-Agent strings into structured browser, OS, and device data without framework coupling. It’s ideal for both frontend and backend use, offers excellent cross-platform support, and gives you full control over how results are used—perfect for feature detection, logging, or conditional logic outside of Express middleware.

README for express-device

express-device Build Status NPM version

Case you're interested only on the device type detection based on the useragent string and don't need all the express related stuff, then use the device package (https://www.npmjs.com/package/device) which was refactored from express-device for that purpose.

why express-device?

I'm really into node.js and lately I've been playing a lot with it. One of the steps I wanted to take in my learning path was to build a node.js module and published it to npm.

Then I had an idea: why not develop a server side library to mimic the behaviour that Twitter's Bootstrap has in order to identify where a browser is running on a desktop, tablet or phone. This is great for a responsive design, but on the server side.

how it came to be?

First I started to search how I could parse the user-agent string and how to differentiate tablets from smartphones. I found a couple good references, such as:

But then I came across with Brett Jankord's blog. He developed Categorizr which is what I was trying to do for node.js but for PHP. He has the code hosted at Github. So, express-device parsing methods (to extract device type) are based on Categorizr. Also I've used all of his user-agent strings compilation to build my unit tests.

how to use it?

From v0.4.0 express-device only works with express >= v4.x.x and node >= v0.10. To install it you only need to do:

$ npm install express-device

Case you're using express 3.x.x you should install version 0.3.13:

$ npm install express-device@0.3.13

Case you're using express 2.x.x you should install version 0.1.2:

$ npm install express-device@0.1.2

express-device is built on top of express framework. Here's an example on how to configure express to use it:

var device = require('express-device');

app.set('view engine', 'ejs');
app.set('view options', { layout: false });
app.set('views', __dirname + '/views');

app.use(bodyParser());
app.use(device.capture());

By doing this you're enabling the request object to have a property called device, which have the following properties:

NameField TypeDescriptionPossible Values
typestringIt gets the device type for the current requestdesktop, tv, tablet, phone, bot or car
namestringIt gets the device name for the current requestExample: iPhone. If the option parseUserAgent is set to false, then it will return an empty string

Since version 0.3.4 you can now override some options when calling device.capture(). It accepts an object with only the config options (the same that the device supports) you which to override (go here for some examples). The ones you don't override it will use the default values. Here's the list with the available config options:

NameField TypeDescriptionPossible Values
emptyUserAgentDeviceTypestringDevice type to be returned whenever the request has an empty user-agent. Defaults to desktop.desktop, tv, tablet, phone, bot or car
unknownUserAgentDeviceTypestringDevice type to be returned whenever the request user-agent is unknown. Defaults to phone.desktop, tv, tablet, phone, bot or car
botUserAgentDeviceTypestringDevice type to be returned whenever the request user-agent belongs to a bot. Defaults to bot.desktop, tv, tablet, phone, bot or car
carUserAgentDeviceTypestringDevice type to be returned whenever the request user-agent belongs to a car. Defaults to car.desktop, tv, tablet, phone, bot or car
parseUserAgentstringConfiguration to parse the user-agent string using the useragent npm package. It's needed in order to get the device name. Defaults to false.true | false

express-device can also add some variables to the response locals property that will help you to build a responsive design:

is_desktopIt returns true in case the device type is "desktop"; false otherwise
is_phoneIt returns true in case the device type is "phone"; false otherwise
is_tabletIt returns true in case the device type is "tablet"; false otherwise
is_mobileIt returns true in case the device type is "phone" or "tablet"; false otherwise
is_tvIt returns true in case the device type is "tv"; false otherwise
is_botIt returns true in case the device type is "bot"; false otherwise
is_carIt returns true in case the device type is "car"; false otherwise
device_typeIt returns the device type string parsed from the request
device_nameIt returns the device name string parsed from the request
In order to enable this method you have to pass the app reference to **device.enableDeviceHelpers(app)**, just before **app.use(app.router)**.

Here's an example on how to use them (using EJS view engine):

<html>
<head>
    <title><%= title %></title>
</head>
<body>
    <h1>Hello World!</h1>
    <% if (is_desktop) { %>
    <p>You're using a desktop</p>
    <% } %>
    <% if (is_phone) { %>
    <p>You're using a phone</p>
    <% } %>
    <% if (is_tablet) { %>
    <p>You're using a tablet</p>
    <% } %>
    <% if (is_tv) { %>
    <p>You're using a tv</p>
    <% } %>
    <% if (is_bot) { %>
    <p>You're using a bot</p>
    <% } %>
    <% if (is_car) { %>
    <p>You're using a car</p>
    <% } %>
</body>
</html>

You can check a full working example here.

In version 0.3.0 a cool feature was added: the ability to route to a specific view\layout based on the device type (you must pass the app reference to device.enableViewRouting(app) to set it up). Consider the code below:

.
|-- views
    |-- phone
    |    |-- layout.ejs
    |    `-- index.ejs
    |-- layout.ejs
    `-- index.ejs

And this code:

var device = require('express-device');

app.set('view engine', 'ejs');
app.set('view options', { layout: true });
app.set('views', __dirname + '/views');

app.use(bodyParser());
app.use(device.capture());

device.enableViewRouting(app);

app.get('/', function(req, res) {
    res.render('index.ejs');
})

If the request comes from a phone device then the response will render views/phone/index.ejs view with views/phone/layout.ejs as layout. If it comes from another type of device then it will render the default views/index.ejs with the default views/index.ejs. Simply add a folder below your views root with the device type code (phone, tablet, tv or desktop) for the device type overrides. Several combinations are supported. Please check the tests for more examples.

You also have an ignore option:

app.get('/', function(req, res) {
    res.render('index.ejs', { ignoreViewRouting: true });
})

There's a way to force a certain type of device in a specific request. In the example I'm forcing a desktop type and the view rendering engine will ignore the parsed type and render as if it was a desktop that made the request. You can use all the supported device types.

app.get('/', function(req, res) {
    res.render('index.ejs', { forceType: 'desktop' });
})

View routing feature uses the express-partials module for layout detection. If you would like to turn it off, you can use the noPartials option (be advised that by doing this you can no longer use the master\partial layout built into express-device, but you can route to full views):

var device = require('express-device'); 

app.set('view engine', 'ejs');
app.set('view options', { layout: true });
app.set('views', __dirname + '/views');

app.use(express.bodyParser());
app.use(device.capture());

device.enableViewRouting(app, {
    "noPartials":true
});

app.get('/', function(req, res) {
    res.render('index.ejs');
})

contributors

where to go from here?

Currently express-device is on version 0.4.2. In order to add more features I'm asking anyone to contribute with some ideas. If you have some of your own please feel free to mention it here.

But I prefer that you make your contribution with some pull requests ;)

license

(The MIT License)

Copyright (c) 2012-2015 Rodrigo Guerreiro

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.