CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-detect-node

Reliable Node.js environment detection library that checks if the global process variable has the specific [[Class]] of 'process' unique to Node.js

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Detect Node

Detect Node is a lightweight JavaScript library that provides reliable Node.js environment detection. It accurately determines whether code is running in a Node.js environment versus a browser or other JavaScript runtime by checking if the global process variable has the specific [[Class]] of 'process' unique to Node.js.

Package Information

  • Package Name: detect-node
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install detect-node

Core Imports

const isNode = require('detect-node');

For ES modules:

import isNode from 'detect-node';

Basic Usage

const isNode = require('detect-node');

if (isNode) {
  console.log("Running under Node.js");
  // Node.js-specific code here
  const fs = require('fs');
  fs.readFileSync('config.json');
} else {
  console.log("Running in browser or other environment");
  // Browser-specific code here
  document.getElementById('app');
}

Architecture

Detect Node uses a unique detection approach that avoids common pitfalls of other environment detection methods:

  • Reliable Detection: Uses Object.prototype.toString.call() to check the [[Class]] property of the process object
  • Build-Tool Friendly: Doesn't directly reference process in a way that causes bundlers to include Node.js polyfills
  • Universal Compatibility: Provides multiple module formats (CommonJS, ES modules, browser-specific)
  • Safe Execution: Uses type checking to avoid errors when process is undefined

Capabilities

Environment Detection

Detects whether the current JavaScript environment is Node.js by checking for the unique [[Class]] property of the Node.js process object.

/**
 * Boolean indicating whether the current environment is Node.js
 * Returns true if running in Node.js, false otherwise
 */
const isNode; // boolean

The detection logic uses:

Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]'

Module Formats:

  • CommonJS (index.js): module.exports = [detection logic]
  • ES Modules (index.esm.js): export default [detection logic]
  • Browser (browser.js): module.exports = false (always returns false for browser builds)

Usage Examples:

// CommonJS
const isNode = require('detect-node');
if (isNode) {
  // Use Node.js APIs
  const path = require('path');
  const __dirname = path.dirname(__filename);
}

// ES Modules
import isNode from 'detect-node';
if (isNode) {
  // Use Node.js APIs
  import { readFile } from 'fs/promises';
  const data = await readFile('data.txt', 'utf8');
}

// Conditional imports based on environment
if (isNode) {
  const nodeSpecificModule = await import('./node-version.js');
} else {
  const browserSpecificModule = await import('./browser-version.js');
}

Build Tool Integration:

The package is designed to work seamlessly with build tools:

  • Webpack: Uses the browser field in package.json to automatically substitute browser.js for client bundles
  • Browserify: Compatible - doesn't trigger inclusion of Node.js polyfills
  • Rollup: Works with both CommonJS and ES module formats
  • Parcel: Automatically handles different module formats based on target environment

Error Handling:

The detection is safe and will never throw errors:

  • Uses typeof process !== 'undefined' check to avoid ReferenceError
  • Falls back to checking 0 if process is undefined
  • Returns false for any non-Node.js environment
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/detect-node@2.1.x
Publish Source
CLI
Badge
tessl/npm-detect-node badge