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 ?
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 ?
if (typeof Window === 'undefined') {
//node
}
else {
//browser
}
Or you can make it into a function.
function isBrowser() {
return typeof Window !== 'undefined';
}
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.
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.