开发者

get information about current commonjs implementation

开发者 https://www.devze.com 2023-03-20 08:11 出处:网络
Is there a standard way of detecting the implementation (node.js, rhino etc) and ideally version of that implementatio开发者_开发百科n in CommonJS.

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消