How to check whether a script is running under Node.js? How to check whether a script is running under Node.js? javascript javascript

How to check whether a script is running under Node.js?


Well there's no reliable way to detect running in Node.js since every website could easily declare the same variables, yet, since there's no window object in Node.js by default you can go the other way around and check whether you're running inside a Browser.

This is what I use for libs that should work both in a Browser and under Node.js:

if (typeof window === 'undefined') {    exports.foo = {};} else {    window.foo = {};}

It might still explode in case that window is defined in Node.js but there's no good reason for someone do this, since you would explicitly need to leave out var or set the property on the global object.

EDIT

For detecting whether your script has been required as a CommonJS module, that's again not easy. Only thing commonJS specifies is that A: The modules will be included via a call to the function require and B: The modules exports things via properties on the exports object. Now how that is implement is left to the underlying system. Node.js wraps the module's content in an anonymous function:

function (exports, require, module, __filename, __dirname) { 

See: https://github.com/ry/node/blob/master/src/node.js#L325

But don't try to detect that via some crazy arguments.callee.toString() stuff, instead just use my example code above which checks for the Browser. Node.js is a way cleaner environment so it's unlikely that window will be declared there.


By looking for CommonJS support, this is how the Underscore.js library does it:

Edit: to your updated question:

(function () {    // Establish the root object, `window` in the browser, or `global` on the server.    var root = this;     // Create a reference to this    var _ = new Object();    var isNode = false;    // Export the Underscore object for **CommonJS**, with backwards-compatibility    // for the old `require()` API. If we're not in CommonJS, add `_` to the    // global object.    if (typeof module !== 'undefined' && module.exports) {            module.exports = _;            root._ = _;            isNode = true;    } else {            root._ = _;    }})();

Example here retains the Module pattern.


I currently stumbled over a wrong detection of Node which is not aware of the Node-environment in Electron due to a misleading feature-detection. The following solutions identify the process-environment explicitly.


Identify Node.js only

(typeof process !== 'undefined') && (process.release.name === 'node')

This will discover if you're running in a Node-process, since process.release contains the "metadata related to the current [Node-]release".

After the spawn of io.js the value of process.release.name may also become io.js (see the process-doc). To proper detect a Node-ready environment i guess you should check as follows:

Identify Node (>= 3.0.0) or io.js

(typeof process !== 'undefined') &&(process.release.name.search(/node|io.js/) !== -1)

This statement was tested with Node 5.5.0, Electron 0.36.9 (with Node 5.1.1) and Chrome 48.0.2564.116.

Identify Node (>= 0.10.0) or io.js

(typeof process !== 'undefined') &&(typeof process.versions.node !== 'undefined')

@daluege's comment inspired me to think about a more general proof. This should working from Node.js >= 0.10. I didn't find a unique identifier for prior versions.


P.s.: I am posting that answer here since the question lead me here, although the OP was searching for an answer to a different question.