A platform detection library that works on nearly all JavaScript platforms.
npx @tessl/cli install tessl/npm-platform@1.3.0Platform.js is a platform detection library that works on nearly all JavaScript platforms. It provides a unified API for detecting browser, operating system, device, and environment information from user agent strings. The library automatically analyzes the runtime environment and exposes detailed platform information through a simple object interface.
npm install platformES6/TypeScript:
import platform from "platform";CommonJS:
const platform = require("platform");
// Or import individual properties
const { name, version, os, parse } = require("platform");AMD/RequireJS:
define(["platform"], function(platform) {
// use platform
});Browser (Global):
<script src="platform.js"></script>
<script>
// platform is available globally
console.log(platform.name);
</script>// Detect current environment automatically
console.log(platform.name); // "Chrome"
console.log(platform.version); // "91.0.4472.124"
console.log(platform.os.family); // "Windows"
console.log(platform.os.version); // "10"
console.log(platform.description); // "Chrome 91.0.4472.124 on Windows 10 64-bit"
// Parse custom user agent string
const info = platform.parse("Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15");
console.log(info.name); // "Safari"
console.log(info.product); // "iPhone"
console.log(info.os.family); // "iOS"
console.log(info.os.version); // "14.6"Access current environment information through the main platform object properties.
/**
* The main platform object containing environment detection information
*/
declare const platform: {
/** The complete platform description string */
description: string | null;
/** The name of the browser's layout engine (e.g., "Blink", "Gecko", "WebKit") */
layout: string | null;
/** The name of the product's manufacturer (e.g., "Apple", "Google", "Microsoft") */
manufacturer: string | null;
/** The name of the browser/environment (e.g., "Chrome", "Firefox", "Safari") */
name: string | null;
/** The alpha/beta release indicator */
prerelease: string | null;
/** The name of the product hosting the browser (e.g., "iPad", "iPhone") */
product: string | null;
/** The browser's user agent string */
ua: string | null;
/** The browser/environment version */
version: string | null;
/** Operating system information object */
os: OSInfo;
/** Parse a custom user agent string */
parse(ua?: string | object): PlatformInfo;
/** Return platform description as string */
toString(): string;
};Access detailed operating system information through the platform.os object.
interface OSInfo {
/** The CPU architecture the OS is built for (32 or 64) */
architecture: number | null;
/** The family/name of the OS (e.g., "Windows", "OS X", "Linux", "iOS") */
family: string | null;
/** The version of the OS */
version: string | null;
/** Returns the OS string representation */
toString(): string;
}Parse custom user agent strings to extract platform information.
/**
* Creates a new platform object by parsing a user agent string or context object
* @param ua - The user agent string or context object (defaults to navigator.userAgent in browsers)
* @returns A platform object with the same structure as the main platform object
*/
function parse(ua?: string | object): PlatformInfo;
interface PlatformInfo {
description: string | null;
layout: string | null;
manufacturer: string | null;
name: string | null;
prerelease: string | null;
product: string | null;
ua: string | null;
version: string | null;
os: OSInfo;
parse(ua?: string | object): PlatformInfo;
toString(): string;
}Usage Examples:
// Parse iPhone user agent
const iphone = platform.parse("Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15");
console.log(iphone.name); // "Safari"
console.log(iphone.product); // "iPhone"
console.log(iphone.manufacturer); // "Apple"
console.log(iphone.os.family); // "iOS"
console.log(iphone.os.version); // "14.6"
// Parse Chrome on Windows user agent
const chrome = platform.parse("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
console.log(chrome.name); // "Chrome"
console.log(chrome.version); // "91.0.4472.124"
console.log(chrome.layout); // "Blink"
console.log(chrome.os.family); // "Windows"
console.log(chrome.os.version); // "10"
console.log(chrome.os.architecture); // 64
// Parse with context object
const customContext = {
navigator: {
userAgent: "Mozilla/5.0 (iPad; CPU OS 14_6 like Mac OS X) AppleWebKit/605.1.15",
platform: "MacIntel"
}
};
const ipad = platform.parse(customContext);
console.log(ipad.product); // "iPad"
console.log(ipad.os.family); // "iOS"Convert platform objects to string representations for display or logging.
/**
* Returns platform description when the platform object is coerced to a string
* @returns The platform description if available, else an empty string
*/
function toString(): string;Usage Examples:
// Main platform object string conversion
console.log(platform.toString());
// "Chrome 91.0.4472.124 on Windows 10 64-bit"
console.log(String(platform));
// "Chrome 91.0.4472.124 on Windows 10 64-bit"
// OS object string conversion
console.log(platform.os.toString());
// "Windows 10 64-bit"
// Parsed platform string conversion
const mobile = platform.parse("Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X)");
console.log(mobile.toString());
// "Safari 14.6 on Apple iPhone (iOS 14.6)"// Detect mobile vs desktop
const isMobile = platform.product &&
/iPad|iPhone|Android|BlackBerry|Windows Phone/i.test(platform.product);
// Detect specific browsers
const isChrome = platform.name === 'Chrome';
const isSafari = platform.name === 'Safari';
const isFirefox = platform.name === 'Firefox';
// Detect operating systems
const isWindows = platform.os.family && platform.os.family.indexOf('Windows') === 0;
const isMac = platform.os.family === 'OS X';
const isLinux = platform.os.family === 'Linux';
const isIOS = platform.os.family === 'iOS';
const isAndroid = platform.os.family === 'Android';
// Check architecture
const is64Bit = platform.os.architecture === 64;// Analyze multiple user agents
const userAgents = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X)",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"
];
const analysis = userAgents.map(ua => {
const info = platform.parse(ua);
return {
browser: info.name,
version: info.version,
os: info.os.family,
device: info.product || 'Desktop',
description: info.toString()
};
});
console.log(analysis);// While platform.js is informational, you can combine with feature detection
function getEnvironmentInfo() {
return {
// Platform detection (informational)
browser: platform.name,
os: platform.os.family,
mobile: platform.product && /mobile|tablet/i.test(platform.product),
// Feature detection (for functionality)
touch: 'ontouchstart' in window,
webgl: !!window.WebGLRenderingContext,
localStorage: typeof localStorage !== 'undefined',
history: !!(window.history && history.pushState)
};
}