Socket
Socket
Sign inDemoInstall

detect-node

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Protect your apps from supply chain attacks

Install

detect-node

Detect Node.JS (as opposite to browser environment) (reliable)

    2.1.0latest
    GitHub
    npm

Version published
Maintainers
1
Weekly downloads
14,442,569
increased by3.29%

Weekly downloads

Package description

What is detect-node?

The detect-node npm package is a simple utility that allows developers to determine if their JavaScript code is running in a Node.js environment as opposed to a browser environment. This can be useful for writing isomorphic code that behaves differently depending on where it is executed.

What are detect-node's main functionalities?

Node.js environment detection

This feature allows the developer to check if the code is running in Node.js. The package exports a boolean value that is true if the environment is Node.js and false otherwise.

const isNode = require('detect-node');
if (isNode) {
  console.log('Running in Node.js');
} else {
  console.log('Running in the browser');
}

Other packages similar to detect-node

Readme

Source

Install

npm install --save detect-node

Usage:

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

if (isNode) {
  console.log("Running under Node.JS");
} else {
  alert("Hello from browser (or whatever not-a-node env)");
}

The check is performed as:

module.exports = false;

// Only Node.JS has a process variable that is of [[Class]] process
try {
 module.exports = Object.prototype.toString.call(global.process) === '[object process]' 
} catch(e) {}

Thanks to Ingvar Stepanyan for the initial idea. This check is both the most reliable I could find and it does not use process env directly, which would cause browserify to include it into the build.

Keywords

FAQs

Last updated on 16 May 2021

Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc