How to safely determine if JavaScript is running in Node.js or the browser
timetler.com
timetler.com
Shouldn't we just be making two slightly different versions?
typeof process !== 'undefined'
&& process.versions
&& process.versions.nodeNot sure how to fix it, because isNaN can be overridden.
// NaN typeof is ... number
typeof value === 'number' && value != value
btw, the original isNaN() doesn't check if a value is NaN, it checks if a value is coercible to a number > isNaN("hello world")
true > var n = 0; window.__defineGetter__('module', function () { return n++; })
> this.module !== module
true foo = foo_lib.import({clientside_api: true});
Now it works every time and it's easy to determine why.In the browser: window, window === this, document, navigator, many JS APIs such as Audio, Canvas, many DOM and CSS objects, etc. The top level is littered with these things.
Node: module, process, require.main === module, Buffer.