Socket
Socket
Sign inDemoInstall

detect-node

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

detect-node

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


Version published
Maintainers
1
Weekly downloads
10,710,458
decreased by-25.72%

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

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 01 Aug 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your 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