{"id":18051,"library":"platform","title":"Platform.js","description":"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.","status":"active","version":"1.3.6","language":"javascript","source_language":"en","source_url":"https://github.com/bestiejs/platform.js","tags":["javascript","environment","platform","ua","useragent"],"install":[{"cmd":"npm install platform","lang":"bash","label":"npm"},{"cmd":"yarn add platform","lang":"bash","label":"yarn"},{"cmd":"pnpm add platform","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Primary usage pattern for Node.js environments and CommonJS modules. The library's `package.json` points to a CJS entry point. Direct ESM import is not officially supported or documented in current versions and may require a build step or Node.js's CJS interoperability.","wrong":"import platform from 'platform';","symbol":"platform","correct":"const platform = require('platform');"},{"note":"Standard usage pattern for environments employing Asynchronous Module Definition (AMD) loaders like RequireJS.","symbol":"platform","correct":"require(['platform'], function(platform) { /* use platform object */ });"},{"note":"When included directly via a script tag in a browser, `platform` is exposed as a global object. There are no named exports for browser global usage.","wrong":"import { platform } from 'platform';","symbol":"platform","correct":"<script src=\"platform.js\"></script>\n// platform is now a global variable"}],"quickstart":{"code":"const platform = require('platform');\n\nconsole.log('--- Current Platform Information ---');\nconsole.log(`Name: ${platform.name}`);\nconsole.log(`Version: ${platform.version}`);\nconsole.log(`Layout: ${platform.layout}`);\nconsole.log(`OS: ${platform.os}`);\nconsole.log(`Description: ${platform.description}`);\n\nconsole.log('\\n--- Parsing a custom User Agent string ---');\nconst 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';\nconst parsedInfo = platform.parse(customUA);\nconsole.log(`Parsed Name: ${parsedInfo.name}`);\nconsole.log(`Parsed Version: ${parsedInfo.version}`);\nconsole.log(`Parsed Layout: ${parsedInfo.layout}`);\nconsole.log(`Parsed OS: ${parsedInfo.os}`);\nconsole.log(`Parsed Description: ${parsedInfo.description}`);","lang":"javascript","description":"This quickstart demonstrates how to initialize the library in a Node.js environment, access current platform properties, and parse a custom user-agent string."},"warnings":[{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":"<=1.3.6"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"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.","cause":"The `platform.js` script was not loaded or executed correctly in a browser environment, or `require('platform')` failed in Node.js/AMD.","error":"ReferenceError: platform is not defined"},{"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.","cause":"The `platform` object itself is undefined, usually because the module failed to load or initialize, or the `require()` call returned `undefined`.","error":"TypeError: Cannot read properties of undefined (reading 'name') (or similar property access errors)"},{"fix":"Run `npm install platform` or `yarn add platform` to install the package and add it to your project's dependencies.","cause":"The package 'platform' is not listed in `package.json` or not installed in `node_modules`.","error":"Error: \"platform\" is not a valid package in the project."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}