env
Runtime environment detection utilities. All properties are evaluated once at module load time.
Properties
| Name | Type | Description |
|---|---|---|
browser | boolean | true when running in a browser main thread (not a Worker) |
node | boolean | true when running in Node.js |
worker | boolean | true when running in a dedicated or shared Web Worker |
serviceWorker | boolean | true when running in a Service Worker |
test | boolean | true when NODE_ENV is 'test' |
dev | boolean | true when NODE_ENV is 'development' |
prod | boolean | true when NODE_ENV is 'production' |
Notes
browserandnodeare mutually exclusive in standard environments.browserchecks bothwindowandwindow.documentto exclude Web Workers, which haveselfbut nodocument.nodechecksprocess.versions.noderather than justprocess, since some bundlers (e.g. Vite) inject a stubprocessobject in browser environments.- At most one of
dev,prod, andtestshould betrueat a time.
Examples
js
import { env } from '@kalisio/common-core'
if (env.browser) {
// browser-only code
}
if (env.node) {
// Node-only code
}