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:
- Bun
- Deno
- Node.js
- workerd
- Browser
- Unknown
| Runtime | Marker |
|---|---|
| Bun | Bun.version is a string |
| Deno | Deno.version.deno is a string |
| Node.js | process.versions.node is a string |
| workerd | The platform user agent contains Cloudflare-Workers or workerd |
| Browser | window 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.