js-env-utility
v2.0.0
Published
A comprehensive utility module to detect the execution environment (Node.js, Deno, Bun, Browser, Web Worker) and platform information.
Maintainers
Readme
JS-Env Utility
A zero-dependency TypeScript utility for detecting the current execution environment: Node.js, Deno, Bun, browsers, and Web Workers.
Features
- Detect Node.js, Deno, Bun, browser, and Web Worker runtimes.
- Detect the operating system from Node.js or a user-agent string.
- Detect browser name and version.
- Check selected web APIs and normalized Network Information.
- Return a fully typed environment summary.
- Ship ESM and TypeScript declarations with no runtime dependencies.
Installation
npm install js-env-utilityThe published runtime supports Node.js 14.18 and newer as well as modern browsers, Deno, and Bun.
Usage
import {
detectPlatform,
detectOperatingSystem,
detectBrowserInfo,
detectApiSupport,
getEnvironmentInfo,
} from 'js-env-utility';
detectPlatform(); // 'Browser'
detectOperatingSystem(); // e.g. 'Windows'
detectBrowserInfo(); // { name: 'Chrome', version: '...' } | null
detectApiSupport(); // API feature flags | null
getEnvironmentInfo(); // typed aggregate objectAll public return values are exported as TypeScript types: Platform, OperatingSystem, NodePlatform, WebOperatingSystem, BrowserName, BrowserInfo, ApiSupport, NetworkInfo, and EnvironmentInfo.
API
isNode(),isDeno(),isBun(): runtime checks. A Node-compatibleprocessobject in Bun or Deno does not produce a false Node.js result.isBrowser(): true in a DOM browser, excluding Web Workers.isWebWorker(): true in DedicatedWorker, SharedWorker, or ServiceWorker global scopes.detectPlatform(): returnsNode.js,Deno,Bun,Browser,Web Worker, orUnknown.detectOperatingSystem(): returns a Node.js platform value or a normalized web OS name.detectOSFromUserAgent(userAgent): pure user-agent OS detection.detectBrowser()/detectBrowserInfo(): browser name and version, ornulloutside a DOM browser.detectApiSupport(): selected web API support, ornulloutside a DOM browser.detectNetworkInfo(): normalized Network Information, ornullwhen unavailable.getEnvironmentInfo(): aggregate environment object.
Development
npm install
npm run check
npm run build
npm run demonpm test— Node.js, Deno/Bun marker, browser, and Web Worker tests.npm run typecheck— strict TypeScript validation.npm run lint— typed ESLint rules.npm run build— ESM bundles and declarations indist/.npm run demo— rebuild the static demo; serve the repository root with any static server.
Browser/Worker detection notes
Browser detection intentionally requires window.document; a Web Worker is detected through WorkerGlobalScope before browser or Node.js checks. Network Information and Notifications are non-standard or permission-dependent and may be unavailable even in a browser.
License
MIT
