How to detect if script is running in browser or in Node.js?

jcubic

I have jasmine test spec file and I want to run it using both node.js and in browser. How can I detect if script is running in node?

Hisham

A couple of ideas:

You can check for the window global object, if it is available then you are in a browser

if (typeof window === 'undefined')
// this is node

Or you can check for the process object, if it is available then you are in node

if(typeof process === 'object')
// this is also node

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to detect if a mocha test is running in node.js?

From Dev

Node.js script is not running

From Dev

Problems running node.js in browser

From Dev

How can a bash script detect if it is running in the background?

From Dev

How to detect user's browser type in server side (node js) from an api call?

From Dev

How to detect if a Node spawned process is still running?

From Dev

Running a node.js script in Komodo console

From Dev

Node.js requiring a script but not running it

From Dev

Running node.js script with forever

From Dev

Bash Script not running on browser

From Dev

How do browsers detect a JS script is not responding?

From Dev

How do browsers detect a JS script is not responding?

From Dev

Simple way to detect browser or script

From Dev

How to detect whether a asp.net script is already running?

From Dev

Accidently running perl-script with sh, how to detect that and take actions?

From Dev

How to have script detect if terminal emulator is running in a desktop session or not?

From Java

How to detect browser with Angular?

From Dev

how to detect Browser with JavaScript?

From Java

How create cookies in browser with node js

From Dev

how to run node.js client on browser

From Dev

How to avoid browser variables in node.js

From Dev

How do I output contents in browser while the script is running?

From Dev

Running Cucumber JS in Browser

From Dev

Node.js forever module - get path to a script that is running?

From Dev

Error: listen EADDRINUSE when running a Node.js script

From Dev

Running a node.js script every 10 seconds

From Dev

Node.js forever module - get path to a script that is running?

From Dev

Getting error while running the node.js server side script

From Dev

Running a python script in the background using child_process (node js)

Related Related

  1. 1

    How to detect if a mocha test is running in node.js?

  2. 2

    Node.js script is not running

  3. 3

    Problems running node.js in browser

  4. 4

    How can a bash script detect if it is running in the background?

  5. 5

    How to detect user's browser type in server side (node js) from an api call?

  6. 6

    How to detect if a Node spawned process is still running?

  7. 7

    Running a node.js script in Komodo console

  8. 8

    Node.js requiring a script but not running it

  9. 9

    Running node.js script with forever

  10. 10

    Bash Script not running on browser

  11. 11

    How do browsers detect a JS script is not responding?

  12. 12

    How do browsers detect a JS script is not responding?

  13. 13

    Simple way to detect browser or script

  14. 14

    How to detect whether a asp.net script is already running?

  15. 15

    Accidently running perl-script with sh, how to detect that and take actions?

  16. 16

    How to have script detect if terminal emulator is running in a desktop session or not?

  17. 17

    How to detect browser with Angular?

  18. 18

    how to detect Browser with JavaScript?

  19. 19

    How create cookies in browser with node js

  20. 20

    how to run node.js client on browser

  21. 21

    How to avoid browser variables in node.js

  22. 22

    How do I output contents in browser while the script is running?

  23. 23

    Running Cucumber JS in Browser

  24. 24

    Node.js forever module - get path to a script that is running?

  25. 25

    Error: listen EADDRINUSE when running a Node.js script

  26. 26

    Running a node.js script every 10 seconds

  27. 27

    Node.js forever module - get path to a script that is running?

  28. 28

    Getting error while running the node.js server side script

  29. 29

    Running a python script in the background using child_process (node js)

HotTag

Archive