Overview
getEnvironment() 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 enum. Both are exported from @bitrix24/b24jssdk and are used internally to gate browser-only behaviour (DOM, localStorage, iframe messaging) away from server contexts.
import { getEnvironment, Environment } from '@bitrix24/b24jssdk'
if (getEnvironment() === Environment.BROWSE) {
// browser-only code (DOM, localStorage, …)
}
Method Signature
enum Environment {
UNKNOWN = 'unknown',
BROWSE = 'browser',
NODE = 'node'
}
getEnvironment(): Environment
Key Concepts
- Detection order.
getEnvironment()first checks for a browser by testingwindowandwindow.document; if both are present it returnsEnvironment.BROWSE. Otherwise it checks for Node.js viaprocess.versions.node; if present it returnsEnvironment.NODE. When neither matches it returnsEnvironment.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. UNKNOWNcovers everything else. Web workers, edge/serverless runtimes, and other hosts without a DOMwindowor a Nodeprocessfall through toEnvironment.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:
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:
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,localStorageavailability) useBrowserinstead. - Prefer it over ad-hoc
typeof windowchecks. Centralising the detection keeps browser/Node branching consistent and matches the checks the SDK itself makes internally. - Treat
UNKNOWNconservatively. When you cannot confirm a browser, avoid DOM andlocalStorageaccess — manyBrowsermethods assume those globals exist and can throw outside a browser.