2

In PHP, there is the PHP_SAPI variable so in PHP i made :

 if('cli' == PHP_SAPI)
 {
   //my code
 }

What is the equivalent in Node.JS ?

lionelp
  • 443
  • 1
  • 7
  • 21
  • i would probably first check the existence of `window` object , also take a look at http://stackoverflow.com/questions/7327164/common-module-in-node-js-and-browser-javascript – Gntem Mar 06 '14 at 19:49
  • You want to detect if your script is running in Node or not? Or you want to detect if Node.js was launched with a browser? If the latter, how are you launching Node.js with a browser? – Brad Mar 06 '14 at 20:30
  • I do 'supervisor -q -w fwk-lpcms index.js' My scripts always use Node.js. In fact, I want to know if we come from a Symfony's console task equivalent or if we just want to display a site. Maybe, the simplest way is just to define a variable isConsole = true at the beginning of my console tasks. – lionelp Mar 08 '14 at 13:34

3 Answers3

0
if (typeof Window === 'undefined') {
    //node
}
else {
    //browser
}

Or you can make it into a function.

function isBrowser() {
    return typeof Window !== 'undefined';
}
pllee
  • 3,909
  • 2
  • 30
  • 33
  • There is no guarantee that Window won't be defined in Node.js – Brad Mar 06 '14 at 20:30
  • @Brad Globals are much harder to define in node than the browser. It obviously can be defined but 99.99% it is not and if it is that code should be deleted. On the contrary I can say there is no guarantee that `process.title` is not defined in the browser. – pllee Mar 06 '14 at 20:40
  • Like I said in my comment above, of course there is no guarantee. But, I have seen plenty of modules define a `window` object in Node.js to shim in behavior. – Brad Mar 06 '14 at 20:41
  • @Brad Just my personal opinion I wouldn't be concerned about that. – pllee Mar 06 '14 at 20:46
0

If you want to detect that you're in Node.js, take a look at process.title.

It's better though to detect whatever capability you need, as you want your code to be compatible with as many runtimes as possible.

http://nodejs.org/api/process.html#process_process_title

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Of course someone could easily set that in the browser too. – OrangeDog Mar 06 '14 at 20:32
  • @OrangeDog Obviously, but someone could set anything, so there is no guaranteed method. `window` gets defined by several NPM modules. I've never seen a browser module go and set `process.title`, but it is certainly possible. – Brad Mar 06 '14 at 20:33
  • Just an fyi for the popular browserify tool code containing `process.title` packaged with browserify will evaluate to a truthy on the browser. – pllee Mar 06 '14 at 20:42
0

Detecting the name of the global variable might be useful:

var global = function(){ return this }()

switch (global.toString().toLowerCase()) {
  case '[object window]':
    return "I'm a browser"
  case '[object global]':
    return "I'm a node.js"
  default:
    return "wat?"
}

You still can fake this, but I doubt that anyone will.

alex
  • 11,935
  • 3
  • 30
  • 42
  • I don't want to distinguish the server side and the client side. I launch my site and it tells to me [object global] with your method so i can't know that it was a command line task more than a website. – lionelp Mar 08 '14 at 13:27