Core concepts

Runtime detection

Learn how detectRuntime() works, including priority order and strict mode.

Runtime detection

Usage

import { detectRuntime } from 'oruntime'

const runtime = detectRuntime()

The return type is:

type Runtime =
  | 'node'
  | 'bun'
  | 'deno'
  | 'workerd'
  | 'browser'
  | 'unknown'

Detection does not read environment variables, access the file system, or request permission.

Detection order

The order prevents runtimes that expose Node-compatible APIs from being misidentified:

  1. Bun
  2. Deno
  3. Node.js
  4. workerd
  5. Browser
  6. Unknown
RuntimeMarker
BunBun.version is a string
DenoDeno.version.deno is a string
Node.jsprocess.versions.node is a string
workerdThe platform user agent contains Cloudflare-Workers or workerd
Browserwindow and document exist

Strict mode

The default result for an unrecognized environment is unknown:

detectRuntime() // 'unknown' when no marker matches

To fail immediately:

detectRuntime({ throwOnUnknown: true })

This throws UnsupportedRuntimeError with code ERR_UNKNOWN_RUNTIME.

Testing runtime-dependent code

Inject markers through the advanced globals option:

const runtime = detectRuntime({
  globals: {
    Bun: { version: '1.2.0' }
  }
})

console.log(runtime) // 'bun'

Use injection in tests; application code should normally rely on globalThis.

Limitations

  • A Web Worker without a specific marker may be unknown.
  • A runtime imitating another runtime's globals follows the first matching marker.
  • Detection identifies a runtime family, not every platform characteristic.

Use capability detection to decide whether an API can be used.