{"id":10794,"library":"environment","title":"JavaScript Runtime and OS Environment Detector","description":"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.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/sindresorhus/environment","tags":["javascript","runtime","environment","env","execution","engine","platform","context","js","typescript"],"install":[{"cmd":"npm install environment","lang":"bash","label":"npm"},{"cmd":"yarn add environment","lang":"bash","label":"yarn"},{"cmd":"pnpm add environment","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is ESM-only (ES Modules) since v1.0.0. Ensure your project is configured for ES modules to use named imports.","wrong":"const { isBrowser } = require('environment');","symbol":"isBrowser","correct":"import { isBrowser } from 'environment';"},{"note":"There is no default export; all checks are exposed as named exports.","wrong":"import environment from 'environment'; environment.isNode;","symbol":"isNode","correct":"import { isNode } from 'environment';"},{"note":"Destructuring named imports directly is the recommended practice for optimal tree-shaking.","wrong":"import * as environment from 'environment'; environment.isMacOs;","symbol":"isMacOs","correct":"import { isMacOs, isWindows } from 'environment';"}],"quickstart":{"code":"import { isBrowser, isNode, isBun, isMacOs, isWindows, isLinux } from 'environment';\n\nfunction displayEnvironmentInfo() {\n  console.log('Checking current environment...');\n\n  if (isBrowser) {\n    console.log('Detected: Web Browser environment.');\n  } else if (isNode) {\n    console.log('Detected: Node.js environment.');\n  } else if (isBun) {\n    console.log('Detected: Bun runtime environment.');\n  } else {\n    console.log('Detected: Unknown or unsupported JavaScript runtime.');\n  }\n\n  console.log('Checking operating system...');\n  if (isMacOs) {\n    console.log('Running on: macOS operating system.');\n  } else if (isWindows) {\n    console.log('Running on: Windows operating system.');\n  } else if (isLinux) {\n    console.log('Running on: Linux operating system.');\n  } else {\n    console.log('Running on: Another operating system or OS not detectable.');\n  }\n}\n\ndisEnvironmentInfo();","lang":"typescript","description":"Demonstrates how to use various named exports (e.g., `isBrowser`, `isNode`, `isMacOs`) to detect the current JavaScript runtime and operating system at execution time."},"warnings":[{"fix":"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.","message":"Over-reliance on runtime checks can lead to less optimized bundles and slower cold starts. The official documentation recommends preferring conditional package exports (via `package.json` `exports` field) and imports for environment-specific code splitting where possible.","severity":"gotcha","affected_versions":"*"},{"fix":"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.","message":"This package is exclusively an ES Module (ESM) and does not provide a CommonJS (CJS) entry point. Attempting to `require()` the package or use `import` statements in a CJS context will result in a runtime error.","severity":"breaking","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"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).","cause":"Attempting to use `import` statements from this ESM-only package within a CommonJS-only Node.js project or script.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"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';`.","cause":"This error typically occurs in TypeScript projects when the `tsconfig.json` `module` option is set to a CommonJS target (e.g., `\"commonjs\"`) but the source code is trying to import an ESM package directly without proper interop or bundling.","error":"TypeError: (0 , environment_1.isBrowser) is not a function"}],"ecosystem":"npm"}