Is there a standard way of detecting the implementation (node.js, rhino etc) and ideally version of that implementatio开发者_开发百科n in CommonJS.
If not, what do people do to get it?
I'm thinking something akin to the HTTP User-Agent
header in the browser world.
No, but you could detect whether you're inside node, for example:
if (typeof process !== 'undefined') {
console.log('node!');
}
If you wanted to be absolutely certain it's node:
if (typeof process !== 'undefined'
&& process && process.versions
&& process.versions.node) {
console.log('node version:', process.version);
}
I can't say for other environments, (I've never used Rhino).
But to answer your question more in-depth, Node doesn't have a strict "version" of CommonJS it implements. Node hasn't been catering to CommonJS specs for a long time now (aside from the recent AMD implementation, which wasn't a full implementation anyway).
精彩评论