is-node

1.1.1 · active · verified Sun Apr 19

This package provides a minimal utility to programmatically determine if the current JavaScript execution environment is Node.js. It achieves this by checking for the presence of the global `process` object and specific properties like `process.versions.node`. The current stable version is 1.1.1. The package maintains a relatively active release cadence, primarily for patch updates related to build processes (e.g., `npm ignore`). Its key differentiator is its singular focus and small footprint, offering a straightforward boolean value rather than a complex function or object. It's commonly employed in isomorphic libraries or application code that needs to adapt behavior between server-side Node.js and client-side browser environments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use the `is-node` boolean to conditionally execute code, logging environment-specific details depending on whether the current runtime is Node.js.

import isNode from 'is-node';

function logEnvironmentDetails() {
  if (isNode) {
    console.log("Detected: Running in a Node.js environment.");
    // Node.js-specific global objects and APIs are available here
    console.log(`Node.js Version: ${process.version}`);
    console.log(`Current Working Directory: ${process.cwd()}`);
    // Example: process.stdout.write('Writing to stdout directly\n');
  } else {
    console.log("Detected: Running in a non-Node.js environment (e.g., browser, Deno, Bun).");
    // Browser-specific APIs would be available here (e.g., document, window)
    // console.log(`User Agent: ${navigator.userAgent}`);
    // document.getElementById('app').innerHTML = 'Hello from the browser!';
  }
}

logEnvironmentDetails();

view raw JSON →