Skip to main content

Built and signed on GitHub Actions

Easily identify details of the current runtime environment (Node.js, browser, Deno, etc.) in JavaScript or TypeScript with this minimal library.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
7 months ago (1.1.0)

@cross/runtime

JSR Version JSR Score

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

Documentation

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

Supported Environments

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 the getsystemInfo function provides a JSON representation.
Built and signed on
GitHub Actions
View transparency log

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";