Skip to content

Environment 1.0.0

The Environment category contains utility functions that help detect and identify the environment in which the code is running, whether it's in the browser, server, or within different types of workers. These functions are useful for providing environment-specific behaviors.

Installation

To install the environment utilities, use one of the following commands, depending on your package manager:

bash
npm install @utilify/environment
bash
yarn add @utilify/environment
bash
pnpm add @utilify/environment

Once installed, you can import the functions into your project, using either ESM or CJS.

Usage

This library supports both ESM and CJS module systems.

typescript
import { isBrowser } from '@utilify/environment';
javascript
const { isBrowser } = require('@utilify/environment');

Overview

Here’s an overview of the functions available in the environment category:

getOS

typescript
function getOS(): string | undefined;

Returns the user's operating system based on the browser's userAgent. Returns undefined if executed in the server.

isBun

typescript
function isBun(): boolean;

Checks if the code is running in the Bun environment by identifying the execution type through the Bun object.

isBrowser

typescript
function isBrowser(): boolean;

Checks if the code is running in a browser environment by checking the type of window.

isDeno

typescript
function isDeno(): boolean;

Checks if the code is running in the Deno environment by verifying the presence of the Deno object.

isDedicatedWorker

typescript
function isDedicatedWorker(): boolean;

Determines if the code is running inside a Dedicated Worker by checking the type of self.

isMobile

typescript
function isMobile(): boolean | undefined;

Checks if the code is running on a mobile device based on the browser's userAgent.

isNode

typescript
function isNode(): boolean;

Determines if the code is running in a Node.js environment by checking the presence of global and process objects.

isServer

typescript
function isServer(): boolean;

Detects if the code is running on the server by checking if the environment is Node.js, Deno, or Bun.

isServiceWorker

typescript
function isServiceWorker(): boolean;

Checks if the code is running inside a Service Worker by using the type of self.

isSharedWorker

typescript
function isSharedWorker(): boolean;

Determines if the code is running inside a Shared Worker by checking the type of self.

isWebWorker

typescript
function isWebWorker(): boolean;

Checks if the code is running inside any type of Web Worker, including Dedicated, Shared, or Service Worker.

Released under the MIT License.