Skip to content

Instantly share code, notes, and snippets.

@janaSunrise
Created September 27, 2022 06:03
Show Gist options
  • Save janaSunrise/98f406dec4e951aac24288397ba522a4 to your computer and use it in GitHub Desktop.
Save janaSunrise/98f406dec4e951aac24288397ba522a4 to your computer and use it in GitHub Desktop.
Check which Javascript runtime is being used (Browser, Node, Deno or Bun).
function isNodeRuntime() {
// The reason we check for both `v8` instead of `node` is because when running `process.versions` under
// Bun, it returns `node` and `bun` version, but it doesn't use v8 engine, hence not a Node.js runtime.
return (
typeof process !== 'undefined' &&
process.versions &&
process.versions.v8
);
}
function isDenoRuntime() {
return typeof Deno !== 'undefined';
}
function isBunRuntime() {
return typeof Bun !== 'undefined';
}
function isBrowser() {
return typeof window !== 'undefined';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment