A lightweight TypeScript utility that detects the current JavaScript runtime environment (Deno, Node.js, Bun, or Browser).
deno add jsr:@online/runtimeimport { EngineType, getRuntime } from "@online/runtime";
const currentRuntime = getRuntime();
switch (currentRuntime) {
case EngineType.Deno:
console.log("Running in Deno");
break;
case EngineType.Node:
console.log("Running in Node.js");
break;
case EngineType.Bun:
console.log("Running in Bun");
break;
case EngineType.Browser:
console.log("Running in Browser");
break;
default:
console.log("Unknown runtime environment");
}An enum representing different JavaScript runtime environments:
enum EngineType {
Deno = "deno",
Node = "node",
Bun = "bun",
Browser = "browser",
Unknown = "unknown",
}Returns the current runtime environment as an EngineType.
Returns:
EngineType.Deno- When running in DenoEngineType.Node- When running in Node.jsEngineType.Bun- When running in BunEngineType.Browser- When running in a browser environmentEngineType.Unknown- When the runtime cannot be determined
The utility determines the runtime environment by checking for the existence of runtime-specific global variables:
- Checks for
Denoglobal object (Deno) - Checks for
Bunglobal object (Bun) - Checks for
process.versions.node(Node.js) - Checks for
windowobject (Browser) - Returns
Unknownif none of the above are detected
This utility is written in TypeScript and includes type definitions out of the box.
MIT (or specify your chosen license)
Contributions are welcome! Please feel free to submit a Pull Request.