npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

js-runtime-environment

v1.0.0

Published

Identify which JavaScript environment your code is running in at runtime — Browser, Node.js, Bun, Deno, Electron, Workers, etc

Readme

js-runtime-environment

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.

Why

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 to fetch 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.

Highlights

  • Feature detection, not UA sniffing. Safe try/catch guards to avoid ReferenceError 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 rich run_env object for advanced logic.
  • UMD / CJS. Works everywhere the same way. No configuration required.
  • Zero deps. Small footprint, fast, and side-effect free.

What it detects

| 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 |

Examples

Node.js

npm i js-runtime-environment
const env = require('js-runtime-environment');
console.log(env.run_env.name); // e.g. "node"

Browser (direct script tag)

<script src="js-runtime-environment.js"></script>
<script>
  console.log(run_env.name);    // "browser"
  console.log(isBrowser());     // true
  console.log(isNode());        // false
</script>

Web Worker

// worker.js
importScripts('js-runtime-environment.js');

postMessage({
  name: run_env.name,          // "web-worker" or "service-worker"
  isWebWorker: isWebWorker()
});

API

run_env (flags & details)

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
  }
}

Flat detection helpers

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()

How it works (brief)

  • 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, and process.platform when present.

Comparison with other libraries

| 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.

Contributing

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.

License

MIT