Toolbox-XToolbox-X
GuardsMiscellaneous Guards

Environment Guards

Runtime type guards to check different environments.

Runtime type guards to check different environments.

isBrowser

Determines if code is running in a web browser environment

Signature

isBrowser(): boolean

Description

Determines if code is running in a web browser environment by checking for:

  • window object
  • document object

Examples

playground.ts
// Environment-specific logic
function getWindowSize() {
  if (isBrowser()) {
    return {
      width: window.innerWidth,
      height: window.innerHeight
    };
  }
  return null;
}

isNode

Determines if code is running in a Node.js environment

Signature

isNode(): boolean

Description

Determines if code is running in a Node.js environment by checking:

  • process object
  • process.versions.node
  • Absence of browser APIs

Examples

playground.ts
// Module loading
function loadModule() {
  if (isNode()) {
    return require('fs');  // Node-specific module
  }
  throw new Error('Node.js required');
}

isEnvironment

Checks if the current Node.js environment matches the specified value (case-sensitive).

Signature

isEnvironment(env: string): boolean

Description

Common values:

  • "development"
  • "production"
  • "test"

Examples

playground.ts
// Environment-specific config
function getConfig() {
  return isEnvironment('production')
    ? productionConfig
    : developmentConfig;
}

Aliases

Main ExportAlias Names
isEnvironmentisExpectedNodeENV, isNodeENV, isNodeEnvironment

Last updated: Sun, Jun 07, 2026 05:56:18PM (UTC)

On this page