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.
npm install environmentVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use various named exports (e.g., `isBrowser`, `isNode`, `isMacOs`) to detect the current JavaScript runtime and operating system at execution time.
Refactor code to leverage `package.json` `exports` field for environment-specific entry points (e.g., `browser`, `node` conditions) or use bundler-specific environment variables for dead code elimination at build time.
Ensure your `package.json` contains `"type": "module"` or rename your script files to `.mjs` to enable ESM. Convert any `require()` calls to `import` statements. If targeting older Node.js versions or CJS environments strictly, consider transpiling with a bundler or using a different package.
Add `"type": "module"` to your `package.json` file or rename your script file to have a `.mjs` extension. Ensure your Node.js version is compatible with ES Modules (Node.js 12+ for basic support, 14+ recommended).
Adjust your `tsconfig.json` to target a modern module system like `"ESNext"` or `"NodeNext"`, or configure your bundler (e.g., Webpack, Rollup, esbuild) to correctly handle ESM imports and transpile them for your target environment. Ensure named imports are correctly destructured, e.g., `import { isBrowser } from 'environment';`.No dependency data recorded yet.