问题
The only way I can kind-of infer whether node.js or io.js is running is to check process.versions.node
. In io.js, I get 1.0.4.
I'm sure there's a better way - anyone know?
回答1:
Now the most reliable solution is to exec node -h
and see if it contains iojs.org
substring. If it does - it's iojs
:
function isIojs(callback) {
require('child_process').exec(process.execPath + ' -h', function(err, help) {
return err ? callback(err) : callback(null, /iojs\.org/.test(help));
});
}
The big minus of such approach - it's asynchronous. So I wrote a small library which is simplifying the job: is-iojs.
But frankly speaking: who knows when node version 1 will be released, maybe never. So I think for now determination based only on process.version
is enough:
var isIojs = parseInt(process.version.match(/^v(\d+)\./)[1]) >= 1;
Also you can check process.execPath
string, but this approach does not works for windows as far as I know.
来源:https://stackoverflow.com/questions/28259154/is-there-a-reliable-way-of-detecting-whether-io-js-or-node-js-is-running