You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
691 B

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