Platform.js
raw JSON →Platform.js is a lightweight JavaScript library designed for comprehensive platform, browser, OS, and device detection across nearly all JavaScript environments, including browsers, Node.js, and AMD loaders. It is currently stable at version 1.3.6, with the README referencing 1.3.5. As part of the BestieJS 'Best in Class' module collection, it emphasizes solid environment support, ES5+ compatibility, and thorough testing. A key differentiator is its detailed output, providing granular information like browser name, version, layout engine, OS, and device manufacturer. However, it explicitly states it is for informational purposes only and not a substitute for robust feature detection or inference checks, which is a crucial distinction from modern approaches to browser capabilities.
Common errors
error ReferenceError: platform is not defined ↓
<script src="platform.js"></script> is correctly placed and accessible in your HTML (before its usage). In Node.js, verify npm install platform was run and the require path is correct. For AMD, confirm the loader configuration is correct. error TypeError: Cannot read properties of undefined (reading 'name') (or similar property access errors) ↓
const platform = require('platform');. For AMD, require(['platform'], function(platform) { ... });. In a browser, ensure the script loaded successfully and no other script overwrote the global platform object. error Error: "platform" is not a valid package in the project. ↓
npm install platform or yarn add platform to install the package and add it to your project's dependencies. Warnings
gotcha Platform.js is for informational purposes only and explicitly states it is not intended as a substitution for feature detection or inference checks. Relying on user-agent strings for feature availability is an anti-pattern. ↓
gotcha User-agent string parsing is inherently fragile and prone to inaccuracies due to browser spoofing, changing UA formats, and new browser releases. The information provided by Platform.js may become outdated or incorrect over time without updates. ↓
gotcha The library primarily uses CommonJS (`require()`) and global browser patterns. While Node.js allows `import` of CJS modules, direct, optimized ESM imports (`import { name } from 'platform'`) are not explicitly supported or documented for this package version, which could affect bundler tree-shaking. ↓
Install
npm install platform yarn add platform pnpm add platform Imports
- platform wrong
import platform from 'platform';correctconst platform = require('platform'); - platform
require(['platform'], function(platform) { /* use platform object */ }); - platform wrong
import { platform } from 'platform';correct<script src="platform.js"></script> // platform is now a global variable
Quickstart
const platform = require('platform');
console.log('--- Current Platform Information ---');
console.log(`Name: ${platform.name}`);
console.log(`Version: ${platform.version}`);
console.log(`Layout: ${platform.layout}`);
console.log(`OS: ${platform.os}`);
console.log(`Description: ${platform.description}`);
console.log('\n--- Parsing a custom User Agent string ---');
const customUA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36 Edg/100.0.1185.36';
const parsedInfo = platform.parse(customUA);
console.log(`Parsed Name: ${parsedInfo.name}`);
console.log(`Parsed Version: ${parsedInfo.version}`);
console.log(`Parsed Layout: ${parsedInfo.layout}`);
console.log(`Parsed OS: ${parsedInfo.os}`);
console.log(`Parsed Description: ${parsedInfo.description}`);