is-node
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
-
TypeError: isNode is not a function
cause `is-node` exports a boolean value directly, not a callable function.fixDo not attempt to call `isNode`. Use it directly as a boolean condition, e.g., `if (isNode) { /* ... */ }`. -
SyntaxError: Named export 'isNode' not found. The requested module 'is-node' does not provide an export named 'isNode'
cause Attempting to use a named import for `is-node`, which is a default export.fixChange the import statement to a default import: `import isNode from 'is-node';`. -
ReferenceError: process is not defined
cause This error occurs if `is-node` is used in a non-Node.js environment where the global `process` object is entirely absent, and an upstream bundler or environment might not have correctly handled its Node.js-specific checks, or if other Node.js-specific code is executed assuming `process` is available.fixEnsure your build process correctly handles Node.js-specific code for different targets. If this error occurs in a browser environment, verify `is-node` itself is evaluating to `false` and that other Node.js assumptions are not present.
Warnings
- gotcha The `is-node` check relies on the global `process` object and specifically `process.versions.node`. While robust for typical Node.js environments, highly specialized or spoofed environments (e.g., some edge runtimes, custom JSDOM setups, or other JavaScript runtimes that partially emulate Node.js) might incorrectly report or fail to report as Node.js.
- breaking Prior to version 1.1.0 (approximately, based on related package updates in the monorepo), the package might not have shipped TypeScript types directly. While not strictly a runtime breaking change, integrating older versions into TypeScript projects would require manual type declarations (`@types/is-node` or custom `d.ts`).
- gotcha This package is designed for a single purpose: detecting the Node.js runtime. It does not provide browser-specific feature detection or differentiate between various non-Node.js runtimes (e.g., Deno, Bun, Web Workers, Electron render process).
Install
-
npm install is-node -
yarn add is-node -
pnpm add is-node
Imports
- isNode
import { isNode } from 'is-node';import isNode from 'is-node';
- isNode
const { isNode } = require('is-node');const isNode = require('is-node'); - isNode
import isNode from 'is-node'; // In TypeScript, 'isNode' is directly typed as a boolean
Quickstart
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();