JavaScript Runtime and OS Environment Detector

1.1.0 · active · verified Sun Apr 19

The `environment` package provides a lightweight and robust utility for runtime detection of the JavaScript execution context. It allows developers to programmatically determine whether their code is running in a web browser, Node.js, Bun, Deno, Electron, jsdom, or various Web Worker environments (dedicated, shared, service). Additionally, it offers checks for common operating systems like macOS, Windows, Linux, iOS, and Android, introduced in version 1.1.0. The current stable version is 1.1.0, with a conservative release cadence that suggests stability. Its primary differentiator is its comprehensive and accurate detection logic for a wide array of JavaScript runtimes and host environments, making it useful for environment-specific configurations or feature toggles, though the documentation encourages preferring conditional exports and imports where possible for better optimization.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use various named exports (e.g., `isBrowser`, `isNode`, `isMacOs`) to detect the current JavaScript runtime and operating system at execution time.

import { isBrowser, isNode, isBun, isMacOs, isWindows, isLinux } from 'environment';

function displayEnvironmentInfo() {
  console.log('Checking current environment...');

  if (isBrowser) {
    console.log('Detected: Web Browser environment.');
  } else if (isNode) {
    console.log('Detected: Node.js environment.');
  } else if (isBun) {
    console.log('Detected: Bun runtime environment.');
  } else {
    console.log('Detected: Unknown or unsupported JavaScript runtime.');
  }

  console.log('Checking operating system...');
  if (isMacOs) {
    console.log('Running on: macOS operating system.');
  } else if (isWindows) {
    console.log('Running on: Windows operating system.');
  } else if (isLinux) {
    console.log('Running on: Linux operating system.');
  } else {
    console.log('Running on: Another operating system or OS not detectable.');
  }
}

disEnvironmentInfo();

view raw JSON →