Platform.js

raw JSON →
1.3.6 verified Sat Apr 25 auth: no javascript

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.

error ReferenceError: platform is not defined
cause The `platform.js` script was not loaded or executed correctly in a browser environment, or `require('platform')` failed in Node.js/AMD.
fix
Ensure <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)
cause The `platform` object itself is undefined, usually because the module failed to load or initialize, or the `require()` call returned `undefined`.
fix
Check the import/require statement. For CommonJS, 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.
cause The package 'platform' is not listed in `package.json` or not installed in `node_modules`.
fix
Run npm install platform or yarn add platform to install the package and add it to your project's dependencies.
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.
fix Always use modern feature detection (e.g., `if ('serviceWorker' in navigator)`) or rely on established polyfills and transpilers for compatibility, rather than inferring capabilities from browser strings.
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.
fix Regularly update the `platform` package to benefit from the latest UA parsing logic. Be aware that even the latest version might not correctly identify very new or niche browsers/OSes immediately. Prioritize feature detection where possible.
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.
fix When using ESM, stick to `import platform from 'platform'` (which will resolve to the CJS default export) or continue using `require('platform')`. If bundle size is a critical concern, consider alternatives that offer explicit ESM support with named exports.
npm install platform
yarn add platform
pnpm add platform

This quickstart demonstrates how to initialize the library in a Node.js environment, access current platform properties, and parse a custom user-agent string.

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}`);