How to check whether a script is running under Nod

2020-01-24 10:24发布

I have a script I am requiring from a Node.js script, which I want to keep JavaScript engine independent.

For example, I want to do exports.x = y; only if it’s running under Node.js. How can I perform this test?


When posting this question, I didn’t know the Node.js modules feature is based on CommonJS.

For the specific example I gave, a more accurate question would’ve been:

How can a script tell whether it has been required as a CommonJS module?

20条回答
何必那么认真
2楼-- · 2020-01-24 10:58
const isNode =
  typeof process !== 'undefined' &&
  process.versions != null &&
  process.versions.node != null;
查看更多
萌系小妹纸
3楼-- · 2020-01-24 10:59

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.

查看更多
不美不萌又怎样
4楼-- · 2020-01-24 11:01

Here's a pretty cool way to do it as well:

const isBrowser = this.window === this;

This works because in browsers the global 'this' variable has a self reference called 'window'. This self reference is not existent in Node.

  • In the browser 'this' is a reference to the global object, called 'window'.
  • In Node 'this' is a reference to the module.exports object.
    • 'this' is not a reference to the Node global object, called 'global'.
    • 'this' is not a reference to the the module variable declaration space.

To break the above suggested browser check you would have to do something like the following

this.window = this;

before executing the check.

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-01-24 11:04

Edit: Regarding your updated question: "How can a script tell whether it has been required as a commonjs module?" I don't think it can. You can check whether exports is an object (if (typeof exports === "object")), since the spec requires that it be provided to modules, but all that tells you is that ... exports is an object. :-)


Original answer:

I'm sure there's some NodeJS-specific symbol (EventEmitter, perhaps no, you have to use require to get the events module; see below) that you could check for, but as David said, ideally you're better off detecting the feature (rather than environment) if it makes any sense to do so.

Update: Perhaps something like:

if (typeof require === "function"
    && typeof Buffer === "function"
    && typeof Buffer.byteLength === "function"
    && typeof Buffer.prototype !== "undefined"
    && typeof Buffer.prototype.write === "function") {

But that just tells you that you're in an environment with require and something very, very much like NodeJS's Buffer. :-)

查看更多
聊天终结者
6楼-- · 2020-01-24 11:06

The following works in the browser unless intentionally,explicitly sabotaged:

if(typeof process === 'object' && process + '' === '[object process]'){
    // is node
}
else{
    // not node
}

Bam.

查看更多
聊天终结者
7楼-- · 2020-01-24 11:06

I'm using process to check for node.js like so

if (typeof(process) !== 'undefined' && process.version === 'v0.9.9') {
  console.log('You are running Node.js');
} else {
  // check for browser
}

or

if (typeof(process) !== 'undefined' && process.title === 'node') {
  console.log('You are running Node.js');
} else {
  // check for browser
}

Documented here

查看更多
登录 后发表回答