Get where your JavaScript runs.
Universal runtime detection for modern JS: Browser, Web Worker, Service Worker, Worklet, Node.js, Node.js Worker, Bun, Deno, Electron (main / renderer), NW.js, React-Native, Vercel Edge, Cloudflare Workers, and more — with a single tiny dependency-free module.
JavaScript today runs in many different contexts: sometimes in the browser, sometimes in a Node.js server, sometimes inside a worker, and increasingly in edge environments like Cloudflare Workers or Vercel Edge.
Often the same codebase needs to run in more than one place, but with slight differences in behavior — for example:
- Load different modules depending on server vs. browser.
- Use
fs
in Node but fall back tofetch
in the browser. - Register event listeners differently in a Service Worker vs. a Web Worker.
js-runtime-environment makes this trivial: you can safely detect the exact runtime and branch your logic without hacks, UA sniffing, or brittle feature checks.
- Feature detection, not UA sniffing. Safe
try/catch
guards to avoidReferenceError
across exotic runtimes. - Broad coverage. Distinguishes Service/Web/Worklet; detects Node main vs worker threads; recognizes Edge runtimes and desktop shells.
- Drop-in API. Flat, top-level detection functions (
isBrowser()
,isNode()
, …) plus a richrun_env
object for advanced logic. - UMD / CJS. Works everywhere the same way. No configuration required.
- Zero deps. Small footprint, fast, and side-effect free.
Category | Detected environments |
---|---|
Browsers | Browser (window+document), JsDom |
Workers | Web Worker, Service Worker, Worklet, Dedicated Worker, Shared Worker |
Server runtimes | Node.js, Node.js Worker, Bun, Deno |
Desktop shells | Electron (main / renderer), NW.js |
Mobile / Hybrid | React-Native |
Edge runtimes | Vercel Edge, Cloudflare Workers |
OS (best-effort) | macOS, Windows, Linux, iOS, Android |
Other | Generic shell fallback |
npm i js-runtime-environment
const env = require('js-runtime-environment');
console.log(env.run_env.name); // e.g. "node"
<script src="js-runtime-environment.js"></script>
<script>
console.log(run_env.name); // "browser"
console.log(isBrowser()); // true
console.log(isNode()); // false
</script>
// worker.js
importScripts('js-runtime-environment.js');
postMessage({
name: run_env.name, // "web-worker" or "service-worker"
isWebWorker: isWebWorker()
});
A structured snapshot computed at load time, plus a normalized name
.
run_env = {
name: string,
web: boolean,
window: boolean,
worker: boolean,
web_worker: boolean,
service_worker: boolean,
worklet: boolean,
nodejs: boolean,
bun: boolean,
deno: boolean,
electron: boolean,
electron_main: boolean,
electron_renderer: boolean,
nwjs: boolean,
node_main: boolean,
node_worker: boolean,
react_native: boolean,
edge_runtime: boolean,
cloudflare_worker: boolean,
shell: boolean,
details: {
node: string|null,
bun: string|null,
deno: string|null,
electron: string|null,
edge: string|null
}
}
All functions return a boolean and are safe across runtimes.
isBrowser()
isNode()
isBun()
isDeno()
isElectron()
isJsDom()
// Workers
isWebWorker()
isDedicatedWorker()
isSharedWorker()
isServiceWorker()
// OS (best-effort)
isMacOs()
isWindows()
isLinux()
isIos()
isAndroid()
- Emphasizes feature detection (e.g.,
skipWaiting
/clients
for Service Workers;importScripts
for Web Workers;WorkletGlobalScope
for Worklets). - Node.js main vs worker via
worker_threads.isMainThread
when available. - Electron via
process.versions.electron
and window presence to split main/renderer. - Edge runtimes and platforms use stable globals (e.g.,
EdgeRuntime
,WebSocketPair
,HTMLRewriter
). - OS checks combine
navigator.userAgentData.platform
,navigator.platform
,userAgent
, andprocess.platform
when present.
Feature / Runtime | js-runtime-environment | environment | wherearewe | runtimey | js-runtime |
---|---|---|---|---|---|
Browser | 🟩 | 🟩 | 🟩 | 🟩 | ⬜ |
JsDom | 🟩 | 🟩 | ⬜ | ⬜ | ⬜ |
Web Worker | 🟩 | 🟩 | 🟩 | 🟩 | ⬜ |
Service Worker | 🟩 | 🟩 | ⬜ | ⬜ | ⬜ |
Worklet | 🟩 | ⬜ | ⬜ | ⬜ | ⬜ |
Node.js | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 |
Node.js Worker | 🟩 | ⬜ | ⬜ | ⬜ | ⬜ |
Bun | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 |
Deno | 🟩 | 🟩 | ⬜ | 🟩 | 🟩 |
Electron (main) | 🟩 | 🟩 | ⬜ | ⬜ | ⬜ |
Electron (renderer) | 🟩 | 🟩 | ⬜ | ⬜ | ⬜ |
NW.js | 🟩 | ⬜ | ⬜ | ⬜ | ⬜ |
React Native | 🟩 | ⬜ | ⬜ | ⬜ | ⬜ |
Vercel Edge Runtime | 🟩 | ⬜ | ⬜ | ⬜ | ⬜ |
Cloudflare Workers | 🟩 | ⬜ | ⬜ | ⬜ | ⬜ |
macOS | 🟩 | 🟩 | ⬜ | ⬜ | ⬜ |
Windows | 🟩 | 🟩 | ⬜ | ⬜ | ⬜ |
Linux | 🟩 | 🟩 | ⬜ | ⬜ | ⬜ |
iOS | 🟩 | 🟩 | ⬜ | ⬜ | ⬜ |
Android | 🟩 | 🟩 | ⬜ | ⬜ | ⬜ |
✅ js-runtime-environment offers the broadest coverage, splitting Node.js main/worker and Electron main/renderer, while also detecting modern edge runtimes, mobile shells, and OS platforms.
Issues and PRs are welcome. Useful areas:
- Additional edge-platform detectors.
- More smoke tests across browsers (incl. SW / WW / Worklet).
- Accuracy refinements for React-Native and desktop shells.
MIT