├── LICENSE ├── Readme.md ├── browser.js ├── index.esm.js ├── index.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Ilya Kantor 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 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. 31 | -------------------------------------------------------------------------------- /browser.js: -------------------------------------------------------------------------------- 1 | module.exports = false; 2 | 3 | -------------------------------------------------------------------------------- /index.esm.js: -------------------------------------------------------------------------------- 1 | // Only Node.JS has a process variable that is of [[Class]] process 2 | export default Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]'; 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // Only Node.JS has a process variable that is of [[Class]] process 2 | module.exports = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]'; 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "detect-node", 3 | "version": "2.1.0", 4 | "description": "Detect Node.JS (as opposite to browser environment) (reliable)", 5 | "main": "index.js", 6 | "module": "index.esm.js", 7 | "browser": "browser.js", 8 | "scripts": { 9 | "test": "echo \"Error: no test specified\" && exit 1" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/iliakan/detect-node" 14 | }, 15 | "keywords": [ 16 | "detect", 17 | "node" 18 | ], 19 | "author": "Ilya Kantor", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/iliakan/detect-node/issues" 23 | }, 24 | "homepage": "https://github.com/iliakan/detect-node" 25 | } 26 | --------------------------------------------------------------------------------