snippetjavascriptTip
Check if the current JavaScript environment is Node.js or a browser
Viewed 0 times
javascriptcheckenvironmentnodethebrowsercurrent
Problem
It's no secret that JavaScript environments are not created equal, with differences in the available APIs, global objects, and even the language itself. This is why it's important to know the current environment in order to avoid errors and unexpected behavior.
In order to determine if the current environment is Node.js, we can use the
> [!NOTE]
>
> As I'm not particularly familiar with runtimes such as Deno or Bun, this check might also return
In order to determine if the current environment is Node.js, we can use the
process global object that provides information about the current Node.js process. We can check if process, process.versions and process.versions.node are defined.> [!NOTE]
>
> As I'm not particularly familiar with runtimes such as Deno or Bun, this check might also return
true in other server environments. Make sure to check the documentation of the runtime you're using to see if this is the case.Solution
const isNode = () =>
typeof process !== 'undefined' &&
!!process.versions &&
!!process.versions.node;
isNode(); // true (Node)
isNode(); // false (browser)> [!NOTE]
>
> As I'm not particularly familiar with runtimes such as Deno or Bun, this check might also return
true in other server environments. Make sure to check the documentation of the runtime you're using to see if this is the case.Browsers environments, on the other hand, are known to always contain the
window and document global objects. We can use this fact to determine if the current environment is a browser. The preferred way to check for the existence of a global object is to use the typeof operator, as it allows us to check for the existence of a global object without throwing a ReferenceError.> [!WARNING]
>
Code Snippets
const isNode = () =>
typeof process !== 'undefined' &&
!!process.versions &&
!!process.versions.node;
isNode(); // true (Node)
isNode(); // false (browser)const isBrowser = () =>
![typeof window, typeof document].includes('undefined');
isBrowser(); // true (browser)
isBrowser(); // false (Node)Context
From 30-seconds-of-code: is-node-or-browser
Revisions (0)
No revisions yet.