Cross-Runtime Environment Detection for JavaScript and TypeScript
This package provides a well defined, cross runtime, way to determine details about the current runtime environment (Deno, Bun, Node.js, or browser) along with detailed browser detection. Since version 1.1.0
, it can also parse a User Agent string to extract OS, Product and Version in a reliable way.
Try it out at https://jsfiddle.net/hexag0n/x9568nmy/.
Part of the @cross suite - check out our growing collection of cross-runtime tools at github.com/cross-org.
import {
CurrentArchitecture,
CurrentOS,
CurrentProduct,
CurrentRuntime,
CurrentVersion,
Runtime
} from "@cross/runtime";
console.log(`Runtime: ${CurrentRuntime}`);
console.log(`OS: ${CurrentOS}`);
console.log(`Architecture: ${CurrentArchitecture}`);
console.log(`Product: ${CurrentProduct}`);
console.log(`Version: ${CurrentVersion}\n`);
if (CurrentRuntime == Runtime.Deno) {
console.log("You're running Deno!");
} else {
console.log("You're not running Deno!");
}
This script results in something like:
Runtime: bun
OS: linux
Architecture: x86_64
Product: bun
Version: 1.0.30
You're not running Deno!
... and an example of parsing User Agent String:
import {
getVersionFromUserAgent,
getProductFromUserAgent,
getOSFromUserAgent
} from "@cross/runtime";
const ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36";
const os = getVersionFromUserAgent(ua);
const product = getProductFromUserAgent(ua);
const version = getOSFromUserAgent(ua);
console.log(`OS: ${os}`);
console.log(`Product: ${product}`);
console.log(`Version: ${version}\n`);
Resulting in:
OS: windows
Product: chrome
Version: 128
Installation
# Pick your runtime and package manager:
npx jsr add @cross/runtime # Node.js
deno add @cross/runtime # Deno
bunx jsr add @cross/runtime # Bun
Runtimes
- Deno
- Bun
- Node.js
- Web browsers (Chrome, Firefox, Edge, Safari, Opera, Brave)
- Edge Functions (Cloudflare Workers, Netlify Edge Functions, Fastly Compute@Edge)
Operating Systems
- Windows
- macOS
- Linux
- Android
- iOS
- Less common Unix variants (AIX, FreeBSD, OpenBSD, etc.)
Browsers
- Chrome
- Firefox
- Edge
- Safari
- Opera
- Brave
Important Notes:
- Additional Functionality: Beyond detection, the
dumpSystemInfo
function logs the information, and thegetsystemInfo
function provides a JSON representation.