Guides

Conditional code

Choose between runtime names and available capabilities.

Conditional code

Prefer capabilities

Check the feature you need directly:

if (runtime.capabilities.fetch) {
  await runtimeFetch(url)
}

This remains correct if a future runtime version adds or removes the API.

Use the name for runtime-specific behavior

switch (runtime.name) {
  case 'workerd':
    console.log('Running in a workerd isolate')
    break
}

Use the runtime name only when logic depends on the execution model itself.

Avoid indirect assumptions

Do not use runtime.name === 'node' as a proxy for Fetch availability. Bun, Deno, Workers, and browsers may all support Fetch.

Require a capability

if (!runtime.capabilities.fetch) {
  throw new UnsupportedCapabilityError('fetch', runtime.name)
}

Use graceful degradation for optional features and explicit errors for required ones.

Conditional imports

oruntime does not resolve runtime-specific modules. Use a dynamic import in the relevant branch:

if (runtime.name === 'node') {
  const { readFile } = await import('node:fs/promises')
  const content = await readFile('config.json', 'utf8')
}

Support for that import then depends on the runtime and bundler.