How to detect different JavaScript runtimes

As more and more JavaScript runtimes become popular, and each runtime has some subtle differences in how modules/functions behave,

if we want to support our JavaScript code in these runtimes, we should be able to detect the runtime.

Ideally we should check the supported feature, instead of the runtimes, but in case if we want to detect the runtimes themselves,

here are the checks we can do

if (globalThis.Bun) {
  // bun 
}

if (globalThis.Deno) {
  // deno
}

if(globalThis.Kiesel) {
  // Kiesel
}

For Node.js, we can try

globalThis.process?.release?.name // returns node

But the caveat here is Bun & Deno will return "node" for globalThis.process?.release?.name, so we should use this along with the other checks above.

Hope this is helpful.

If you find my work helpful, You can buy me a coffee.