UNPKG

691 BMarkdownView Raw
1### Install
2
3```shell
4npm install --save detect-node
5```
6
7### Usage:
8
9```js
10var isNode = require('detect-node');
11
12if (isNode) {
13 console.log("Running under Node.JS");
14} else {
15 alert("Hello from browser (or whatever not-a-node env)");
16}
17```
18
19The check is performed as:
20```js
21module.exports = false;
22
23// Only Node.JS has a process variable that is of [[Class]] process
24try {
25 module.exports = Object.prototype.toString.call(global.process) === '[object process]'
26} catch(e) {}
27
28```
29
30Thanks 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.