Skip to content

env

Runtime environment detection utilities. All properties are evaluated once at module load time.

Properties

NameTypeDescription
browserbooleantrue when running in a browser main thread (not a Worker)
nodebooleantrue when running in Node.js
workerbooleantrue when running in a dedicated or shared Web Worker
serviceWorkerbooleantrue when running in a Service Worker
testbooleantrue when NODE_ENV is 'test'
devbooleantrue when NODE_ENV is 'development'
prodbooleantrue when NODE_ENV is 'production'

Notes

  • browser and node are mutually exclusive in standard environments.
  • browser checks both window and window.document to exclude Web Workers, which have self but no document.
  • node checks process.versions.node rather than just process, since some bundlers (e.g. Vite) inject a stub process object in browser environments.
  • At most one of dev, prod, and test should be true at a time.

Examples

js
import { env } from '@kalisio/common-core'

if (env.browser) {
  // browser-only code
}

if (env.node) {
  // Node-only code
}