Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
45
rated 0 times [  50] [ 5]  / answers: 1 / hits: 29291  / 9 Years ago, thu, december 31, 2015, 12:00:00

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?


More From » node.js

 Answers
18

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

[#63878] Tuesday, December 29, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lizet

Total Points: 479
Total Questions: 96
Total Answers: 113

Location: Mexico
Member since Sun, Jul 25, 2021
3 Years ago
;