---
title: "Environment"
description: "Runtime environment detection — `getEnvironment()` and the `Environment` enum for branching between browser and Node.js code paths."
canonical_url: "https://bitrix24.github.io/b24jssdk/docs/working-with-the-rest-api/tools-environment"
last_updated: "2026-07-03"
---
# Environment

> Runtime environment detection — `getEnvironment()` and the `Environment` enum for branching between browser and Node.js code paths.

## Overview

`getEnvironment()`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""} reports whether the code is running in a browser, in Node.js, or in an environment the SDK cannot classify. It returns a member of the `Environment`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""} enum. Both are exported from `@bitrix24/b24jssdk` and are used internally to gate browser-only behaviour (DOM, `localStorage`, iframe messaging) away from server contexts.

```ts
import { getEnvironment, Environment } from '@bitrix24/b24jssdk'

if (getEnvironment() === Environment.BROWSE) {
  // browser-only code (DOM, localStorage, …)
}
```

## Method Signature

```ts-type
enum Environment {
  UNKNOWN = 'unknown',
  BROWSE = 'browser',
  NODE = 'node'
}

getEnvironment(): Environment
```

## Key Concepts

- **Detection order.** `getEnvironment()` first checks for a browser by testing `window` and `window.document`; if both are present it returns `Environment.BROWSE`. Otherwise it checks for Node.js via `process.versions.node`; if present it returns `Environment.NODE`. When neither matches it returns `Environment.UNKNOWN`.
- **The browser member is `BROWSE`, its value is `'browser'`.** Compare against the enum member (`Environment.BROWSE`), not a bare string, so a rename stays type-safe.
- **`UNKNOWN` covers everything else.** Web workers, edge/serverless runtimes, and other hosts without a DOM `window` or a Node `process` fall through to `Environment.UNKNOWN` — treat it as "assume no browser globals".

## Error Handling

`getEnvironment()` never throws. Every check uses `typeof` guards, so a missing `window` or `process` degrades to the next branch rather than raising a `ReferenceError`.

## Examples

Branch between browser and Node.js:

```ts
import { getEnvironment, Environment } from '@bitrix24/b24jssdk'

switch (getEnvironment()) {
  case Environment.BROWSE:
    // Safe to touch the DOM / localStorage here
    break
  case Environment.NODE:
    // Server-side path (e.g. read env vars, use a webhook client)
    break
  default:
    // Environment.UNKNOWN — avoid browser and Node-specific globals
    break
}
```

Guard browser-only work before it runs:

```ts
import { getEnvironment, Environment } from '@bitrix24/b24jssdk'

function isBrowser(): boolean {
  return getEnvironment() === Environment.BROWSE
}

if (isBrowser()) {
  // window / document are available
}
```

## Alternatives and Recommendations

- **Use `getEnvironment()` for a coarse "where am I running" branch.** For finer browser/OS/capability checks (Chrome vs Safari, iOS, touch, `localStorage` availability) use [`Browser`](https://bitrix24.github.io/b24jssdk/raw/docs/working-with-the-rest-api/tools-browser.md) instead.
- **Prefer it over ad-hoc `typeof window` checks.** Centralising the detection keeps browser/Node branching consistent and matches the checks the SDK itself makes internally.
- **Treat `UNKNOWN` conservatively.** When you cannot confirm a browser, avoid DOM and `localStorage` access — many `Browser` methods assume those globals exist and can throw outside a browser.

## Sitemap

See the full [sitemap](/b24jssdk/sitemap.md) for all pages.
