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