Getting started

Quick start

Detect the runtime and start using the main APIs in a few minutes.

Quick start

1. Create an adapter

import { createRuntimeAdapter } from 'oruntime'

const runtime = createRuntimeAdapter()
console.log(runtime.name)
console.log(runtime.version)

name is always defined. version is undefined when no reliable runtime version is available.

2. Read the environment

const mode = runtime.getEnv('NODE_ENV')
const environment = runtime.getEnv()
  • With a key, getEnv() returns string | undefined.
  • Without a key, it returns a copy of all accessible variables.
  • Browsers and Workers return undefined for a key and {} for the full environment.
  • Deno may throw PermissionDeniedError when permission is missing.

3. Read arguments

const args = runtime.argv()

Only user arguments are returned. On Node.js and Bun, the executable and script entries are removed; this matches Deno.args.

4. Check a capability

import { hasRuntimeCapability } from 'oruntime'

if (runtime.capabilities.fetch) {
  // Fetch and AbortController are available.
}

if (hasRuntimeCapability('webStreams')) {
  // ReadableStream and WritableStream are available.
}

5. Make a timed request

import { fetchJSON } from 'oruntime'

const user = await fetchJSON<{ id: number; name: string }>(
  'https://api.example.com/users/1',
  { timeout: 5_000 }
)

Non-2xx responses throw HTTPResponseError; an expired deadline throws TimeoutError.

Complete example

import {
  RuntimeError,
  createRuntimeAdapter,
  fetchJSON
} from 'oruntime'

const runtime = createRuntimeAdapter({ throwOnUnknown: true })

console.log({
  name: runtime.name,
  version: runtime.version,
  cwd: runtime.cwd(),
  argv: runtime.argv()
})

if (!runtime.capabilities.fetch) {
  throw new Error('Fetch is unavailable')
}

try {
  console.log(await fetchJSON('https://example.com/data', { timeout: 3_000 }))
} catch (error) {
  if (error instanceof RuntimeError) console.error(error.code, error.cause)
  else throw error
}