
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
node-device-detector
Advanced tools
Last update: 18/08/2022
Port php lib matomo-org/device-detector to NodeJs
npm install node-device-detector --save
or
yarn add node-device-detector
const DeviceDetector = require('node-device-detector');
const detector = new DeviceDetector({
  clientIndexes: true,
  deviceIndexes: true,
  deviceAliasCode: false,
});
const userAgent = 'Mozilla/5.0 (Linux; Android 5.0; NX505J Build/KVT49L) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.78 Mobile Safari/537.36';
const result = detector.detect(userAgent);
console.log('result parse', result);
PS: When creating an object
detector = new DeviceDetector;data for parsing is reloaded from files, consider this, the best option is initialization at application start I recommend seeing examples
{ 
  os: { 
    name: 'Android',            // os name       
    short_name: 'AND',          // os short code name (format A-Z0-9{3})
    version: '5.0',             // os version
    platform: '',               // os platform (x64, x32, amd etc.)
    family: 'Android'           // os family
  },
  client:  { 
    type: 'browser',            // client type 
    name: 'Chrome Mobile',      // client name name
    short_name: 'CM',           // client short code name (only browser, format A-Z0-9{2,3})
    version: '43.0.2357.78',    // client version
    engine: 'Blink',            // client engine name (only browser)
    engine_version: ''          // client engine version (only browser)
    family: 'Chrome'            // client family (only browser)
  },
  device: { 
    id: 'ZT',                   // short code device brand name (format A-Z0-9{2,3})
    type: 'smartphone',         // device type
    brand: 'ZTE',               // device brand name
    model: 'Nubia Z7 max'       // device model name
    code: 'NX505J'              // device model code  (only result for enable detector.deviceAliasCode) 
  }
}
Result parse empty
{ 
  os: {},                      // empty objects its os not found
  client: {},                  // empty objects its client not found
  device: {      
    id: '',                    // empty string its device brand not found
    type : 'device type',      // device type or empty string
    brand: '',                 // empty string its device brand not found
    model: ''                  // empty string its device model not found
  }
}
const DeviceDetector = require('node-device-detector');
const DeviceHelper = require('node-device-detector/helper');
const detector = new DeviceDetector;
const userAgent = 'Mozilla/5.0 (Linux; Android 5.0; NX505J Build/KVT49L) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.78 Mobile Safari/537.36';
const result = detector.detect(userAgent);
/* check device type (feature phone, smartphone or phablet) */
DeviceHelper.isMobile(result);
/* check device type is desktop */
DeviceHelper.isDesktop(result);
/* check device type is tablet  */
DeviceHelper.isTablet(result);
/* check device type car (side panel in car)  */
DeviceHelper.isCar(result);
/* check device type feature phone (push-button telephones)  */
DeviceHelper.isFeaturePhone(result);
/* check device type smartphone  */
DeviceHelper.isSmartphone(result);
/* check device type phablet  */
DeviceHelper.isPhablet(result);
/* check device type game console (xBox, PlayStation, Nintendo etc)  */
DeviceHelper.isConsole(result);
/* check device type smart speaker (Alisa, Alexa, HomePod etc) */
DeviceHelper.isSmartSpeaker(result);
/* check device type SmartTV/TV box */
DeviceHelper.isTv(result);
/* check device type portable camera */
DeviceHelper.isCamera(result);
/* portable terminal, portable projector */
DeviceHelper.isPeripheral(result);
/* LCD panel or interactive panel  */
DeviceHelper.isSmartDisplay(result);
/* check device type boxes, blu-ray players */
DeviceHelper.isPortableMediaPlayer(result);
/* check device type watches, headsets */
DeviceHelper.isWearable(result);
/* result device type number id */
DeviceHelper.getDeviceTypeId(result);
/* result device type string */
DeviceHelper.getDeviceType(result);
/* result client type string */
DeviceHelper.getClientType(result);
const DeviceDetector = require('node-device-detector');
const DeviceHelper   = require('node-device-detector/helper');
const ClientHints    = require('node-device-detector/client-hints')
const detector = new DeviceDetector({
  clientIndexes: true,
  deviceIndexes: true,
  deviceAliasCode: false,
  // ... all options scroll to Setter/Getter/Options
});
const clientHints = new ClientHints;
const userAgent = res.headers['user-agent'];
const clientHintData = clientHints.parse(res.headers);
const result = detector.detect(userAgent, clientHintData);
// result promise
// added for 2.0.4 version or later
const result = detector.detectAsync(userAgent, clientHintData);
const DeviceDetector = require('node-device-detector');
const userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25 (compatible; Googlebot-Mobile/2.1; +http://www.google.com/bot.html)';
const detector = new DeviceDetector();
const result = detector.parseBot(userAgent);
const DeviceDetector = require('node-device-detector');
const userAgent = 'Mozilla/5.0 (Linux; Android 5.0; NX505J Build/KVT49L) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.78 Mobile Safari/537.36';
const detector = new DeviceDetector({
  clientIndexes: true,
  deviceIndexes: true,
  deviceAliasCode: false,
});
const result = detector.parseOs(userAgent/*, clientHintData*/);
console.log('Result parse os', result);  
const DeviceDetector = require('node-device-detector');
const userAgent = 'Mozilla/5.0 (Linux; Android 5.0; NX505J Build/KVT49L) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.78 Mobile Safari/537.36';
const detector = new DeviceDetector({
    clientIndexes: true,
    deviceIndexes: true,
    deviceAliasCode: false,
});
const result = detector.parseClient(userAgent/*, clientHintData*/);
console.log('Result parse client', result);
const DeviceDetector = require('node-device-detector');
const userAgent = 'Mozilla/5.0 (Linux; Android 5.0; NX505J Build/KVT49L) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.78 Mobile Safari/537.36';
const detector = new DeviceDetector({
  clientIndexes: true,
  deviceIndexes: true,
  deviceAliasCode: false,
});
const resultOs = detector.parseOs(userAgent);
const resultClient = detector.parseClient(userAgent);
const resultDeviceType = detector.parseDeviceType(
 userAgent,
 resultOs,
 resultClient,
 {},
 /*, clientHintData */
);
const result = Object.assign({os:resultOs}, {client:resultClient}, {device: resultDeviceType});
console.log('Result parse lite', result);
const detector = new DeviceDetector({
  osVersionTruncate: 0,      // Truncate OS version from 5.0 to 5 (default '' or null)
  clientVersionTruncate: 2,  // Truncate Client version Chrome from 43.0.2357.78 to 43.0.2357 (default '' or null)
  deviceIndexes: true,       // Using indexes for faster device search (default false)
  clientIndexes: true,       // Using indexes for faster client search (default false)
  deviceAliasCode: false,    // adds the device code to result device.code as is (default false)
});
// You can override these settings at any time using special setters, example
detector.osVersionTruncate = 0;
detector.clientVersionTruncate = 2;
detector.deviceIndexes = true;
detector.clientIndexes = true;
detector.deviceAliasCode = true;
// Array available device types
detector.getAvailableDeviceTypes();
// Array available devices brands
detector.getAvailableBrands();
// Array available browsers
detector.getAvailableBrowsers();
const AliasDevice = require('node-device-detector/parser/device/alias-device');
const userAgent = 'Mozilla/5.0 (Linux; Android 5.0; NX505J Build/KVT49L) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.78 Mobile Safari/537.36';
const aliasDevice = new AliasDevice;
const result = aliasDevice.parse(userAgent);
console.log('Result parse code model', result);
/*
result 
{
  name: "NX505J"
}
is not parse result  {name: ""}
*/
node tests/banchmark.js test result:
Test Mozilla/5.0 (Linux; Android 5.0; NX505J Build/KVT49L) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.78 Mobile Safari/537.36
-----
detector.parseDevice (deviceIndexes on) x 10,449 ops/sec ±0.27% (93 runs sampled)
detector.parseDevice (deviceIndexes off) x 261 ops/sec ±88.58% (92 runs sampled)
detector.parseClient (clientIndexes on) x 1,703 ops/sec ±0.36% (92 runs sampled)
detector.parseClient (clientIndexes off) x 859 ops/sec ±0.46% (93 runs sampled)
detector.parseOS x 10,034 ops/sec ±0.23% (94 runs sampled)
detector.detect (indexes off) x 254 ops/sec ±0.46% (85 runs sampled)
detector.detect (indexes on) x 1,114 ops/sec ±1.44% (91 runs sampled)
Test Mozilla/5.0 (Linux; Android 12; M2101K9AG Build/SKQ1.210908.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/102.0.5005.125 Mobile Safari/537.36 UCURSOS/v1.6_273-android
-----
detector.parseDevice (deviceIndexes on) x 5,457 ops/sec ±0.23% (95 runs sampled)
detector.parseDevice (deviceIndexes off) x 220 ops/sec ±31.15% (87 runs sampled)
detector.parseClient (clientIndexes on) x 5,797 ops/sec ±0.32% (92 runs sampled)
detector.parseClient (clientIndexes off) x 6,243 ops/sec ±0.47% (93 runs sampled)
detector.parseOS x 7,570 ops/sec ±0.92% (93 runs sampled)
detector.detect (indexes off) x 203 ops/sec ±78.87% (86 runs sampled)
detector.detect (indexes on) x 1,695 ops/sec ±1.49% (88 runs sampled)
Test Mozilla/5.0 (Linux; Android 8.0.0; RNE-L21) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36
-----
detector.parseDevice (deviceIndexes on) x 2,315 ops/sec ±0.62% (94 runs sampled)
detector.parseDevice (deviceIndexes off) x 448 ops/sec ±78.47% (89 runs sampled)
detector.parseClient (clientIndexes on) x 1,664 ops/sec ±0.69% (92 runs sampled)
detector.parseClient (clientIndexes off) x 844 ops/sec ±1.09% (93 runs sampled)
detector.parseOS x 10,258 ops/sec ±0.31% (95 runs sampled)
detector.detect (indexes off) x 254 ops/sec ±48.42% (89 runs sampled)
detector.detect (indexes on) x 808 ops/sec ±0.40% (92 runs sampled)
-----
Test Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.44
-----
detector.parseDevice (deviceIndexes on) x 8,387 ops/sec ±1.21% (94 runs sampled)
detector.parseDevice (deviceIndexes off) x 8,645 ops/sec ±0.32% (95 runs sampled)
detector.parseClient (clientIndexes on) x 1,751 ops/sec ±1.87% (91 runs sampled)
detector.parseClient (clientIndexes off) x 1,227 ops/sec ±0.57% (93 runs sampled)
detector.parseOS x 4,921 ops/sec ±0.25% (97 runs sampled)
detector.detect (indexes off) x 799 ops/sec ±1.04% (92 runs sampled)
detector.detect (indexes on) x 1,032 ops/sec ±0.61% (94 runs sampled)
-----
This parser is experimental and contains few devices. (1767 devices, alias devices 3815)
| Brand | Device count | Alias count | - | Brand | Device count | Alias count | 
|---|---|---|---|---|---|---|
| 360 | 12 | 13 | - | 8848 | 4 | 0 | 
| 2e | 2 | 2 | - | 3gnet | 0 | 1 | 
| 3q | 14 | 62 | - | 4good | 10 | 1 | 
| 4ife | 0 | 1 | - | a1 | 0 | 1 | 
| accent | 0 | 5 | - | ace | 8 | 0 | 
| acer | 5 | 68 | - | acteck | 0 | 0 | 
| advan | 0 | 1 | - | advance | 0 | 14 | 
| afrione | 0 | 2 | - | agm | 4 | 0 | 
| ainol | 2 | 16 | - | airness | 0 | 0 | 
| airo wireless | 1 | 0 | - | airties | 0 | 0 | 
| ais | 0 | 2 | - | aiuto | 0 | 0 | 
| aiwa | 0 | 0 | - | akai | 2 | 5 | 
| alba | 0 | 1 | - | alcatel | 29 | 433 | 
| alcor | 1 | 0 | - | alfawise | 0 | 0 | 
| aligator | 0 | 0 | - | allcall | 0 | 3 | 
| alldocube | 2 | 6 | - | allview | 0 | 46 | 
| allwinner | 0 | 3 | - | altech uec | 0 | 0 | 
| altek | 1 | 0 | - | altice | 0 | 0 | 
| altron | 0 | 1 | - | amazon | 19 | 30 | 
| amgoo | 2 | 15 | - | amigoo | 0 | 0 | 
| amoi | 62 | 2 | - | andowl | 0 | 0 | 
| anry | 0 | 0 | - | ans | 0 | 0 | 
| aoc | 0 | 0 | - | aoson | 0 | 6 | 
| apple | 46 | 44 | - | archos | 89 | 7 | 
| arian space | 4 | 2 | - | ark | 1 | 36 | 
| armphone | 0 | 0 | - | arnova | 0 | 36 | 
| arris | 0 | 0 | - | artel | 0 | 2 | 
| artizlee | 0 | 1 | - | asano | 0 | 1 | 
| asanzo | 1 | 0 | - | ask | 0 | 0 | 
| assistant | 2 | 19 | - | asus | 81 | 230 | 
| at&t | 1 | 2 | - | atom | 0 | 3 | 
| atvio | 0 | 0 | - | avenzo | 1 | 3 | 
| avh | 1 | 0 | - | avvio | 3 | 2 | 
| axxion | 0 | 0 | - | azumi mobile | 0 | 1 | 
| bangolufsen | 0 | 0 | - | barnes & noble | 1 | 6 | 
| bb mobile | 2 | 10 | - | beeline | 11 | 1 | 
| bellphone | 1 | 1 | - | benq | 0 | 1 | 
| beyond | 0 | 7 | - | bezkam | 1 | 0 | 
| bigben | 1 | 0 | - | bihee | 2 | 1 | 
| billion | 1 | 1 | - | bird | 1 | 0 | 
| bitel | 4 | 1 | - | bitmore | 2 | 1 | 
| bkav | 1 | 0 | - | black bear | 2 | 0 | 
| black fox | 18 | 12 | - | blackview | 15 | 9 | 
| blu | 13 | 15 | - | bravis | 24 | 17 | 
| clarmin | 3 | 0 | - | colors | 7 | 2 | 
| digifors | 1 | 1 | - | engel | 1 | 1 | 
| firefly mobile | 4 | 1 | - | formuler | 2 | 0 | 
| geotel | 3 | 0 | - | gionee | 4 | 0 | 
| 3 | 5 | - | hisense | 2 | 0 | |
| hotwav | 18 | 1 | - | huawei | 226 | 586 | 
| i-mobile | 1 | 0 | - | imo mobile | 5 | 0 | 
| infinix | 24 | 38 | - | inoi | 4 | 0 | 
| intex | 9 | 3 | - | ipro | 6 | 7 | 
| irbis | 15 | 0 | - | kurio | 3 | 3 | 
| lg | 127 | 286 | - | malata | 1 | 0 | 
| maze | 4 | 0 | - | minix | 1 | 1 | 
| mivo | 3 | 2 | - | mobicel | 3 | 1 | 
| motorola | 27 | 24 | - | noa | 1 | 0 | 
| nomi | 1 | 1 | - | nuu mobile | 9 | 3 | 
| nuvo | 3 | 2 | - | oneplus | 18 | 48 | 
| oppo | 90 | 180 | - | oukitel | 8 | 0 | 
| öwn | 1 | 2 | - | panasonic | 5 | 8 | 
| pipo | 5 | 0 | - | realme | 65 | 94 | 
| samsung | 167 | 714 | - | sony | 44 | 172 | 
| supra | 1 | 0 | - | tecno mobile | 91 | 131 | 
| tiphone | 1 | 0 | - | utok | 1 | 0 | 
| uz mobile | 1 | 0 | - | vernee | 9 | 2 | 
| vivo | 185 | 266 | - | walton | 13 | 0 | 
| we | 8 | 0 | - | weimei | 1 | 0 | 
| wiko | 6 | 5 | - | wileyfox | 9 | 0 | 
| wink | 4 | 0 | - | zync | 2 | 0 | 
| zyq | 1 | 13 | - | 
const InfoDevice = require('node-device-detector/parser/device/info-device');
const infoDevice = new InfoDevice;
const result = infoDevice.info('Asus', 'Zenfone 4');
console.log('Result information', result);
/*
result
{
  display: {
    size: '5.5',
    resolution: '1080x1920',  // width+height
    ratio: '16:9',
    ppi: "401"
  },
  size: '155.4x75.2x7.7',    // width+height+thickness
  weight: '165',
  hardware: {
    // ...
  }
  os: "Android 7.1",
  release: "2017.08",
  sim": "2",
}
is not found result null
*/
Cast methods
const InfoDevice = require('node-device-detector/parser/device/info-device');
const infoDevice = new InfoDevice;
infoDevice.setSizeConvertObject(true);
infoDevice.setResolutionConvertObject(true);
const result = infoDevice.info('Asus', 'Zenfone 4');
console.log('Result information', result);
/*
result
{  
  display: {
    size: "5.5",  // value in inchs
    resolution: {
      width: "1080", // value in px
      height: "1920" // value in px
    },
    ratio: "16:9",   // calculated field
    ppi: "401"       // calculated field
  },
  hardware: {
    ram: "4096",   // RAM value in megabytes
    cpu_id: 19,  // id cpu model in collection
    cpu: {
      name: "Qualcomm Snapdragon 630",  // brand + name
      type: "ARM",                      // architecture type 
      cores: "8",                       // number of cores / threads 
      clock_rate: 2200,                 // value in MHz
      gpu_id: 16                        // id gpu model in collection
	},
    gpu: {
      name: "Qualcomm Adreno 508",
      clock_rate: 650
    }
  },
  os: "Android 7.1",   // initial OS version
  release: "2017.08",  // date release or anonce
  sim": "2",           // count SIM 
  size: {           
    width: "75.2",     // physical width in millimeters
    height: "155.4",   // physical height in millimeters
    thickness: "7.7"   // physical thickness in millimeters
  },
  weight: "165"        // in grams
};
*/
| type | id | 
|---|---|
| desktop | 0 | 
| smartphone | 1 | 
| tablet | 2 | 
| feature phone | 3 | 
| console | 4 | 
| tv | 5 | 
| car browser | 6 | 
| smart display | 7 | 
| camera | 8 | 
| portable media player | 9 | 
| phablet | 10 | 
| smart speaker | 11 | 
| wearable | 12 | 
| peripheral | 13 | 
FAQs
Nodejs device detector (port matomo-org/device-detector)
The npm package node-device-detector receives a total of 22,248 weekly downloads. As such, node-device-detector popularity was classified as popular.
We found that node-device-detector demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.