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
Operating Systems
Browsers
Important Notes:
dumpSystemInfo
function logs the information, and the getsystemInfo
function provides a JSON representation.Add Package
deno add jsr:@cross/runtime
Import symbol
import * as runtime from "@cross/runtime";
---- OR ----
Import directly with a jsr specifier
import * as runtime from "jsr:@cross/runtime";
Add Package
npx jsr add @cross/runtime
Import symbol
import * as runtime from "@cross/runtime";
Add Package
yarn dlx jsr add @cross/runtime
Import symbol
import * as runtime from "@cross/runtime";
Add Package
pnpm dlx jsr add @cross/runtime
Import symbol
import * as runtime from "@cross/runtime";
Add Package
bunx jsr add @cross/runtime
Import symbol
import * as runtime from "@cross/runtime";