Socket.IO 版本输出 [英] Socket.IO Version Output
问题描述
我将 socket.io 从 0.9.16 升级到 1.0.6,输出的版本是这样的:
I upgraded socket.io from 0.9.16 to 1.0.6, and used to output the version like this:
var io = require('socket.io');
console.log("**Socket.IO Version: "+io.version);
并且会给我
**Socket.IO Version: 0.9.16
更新到 1.0.6 后,我得到:
after I updated to 1.0.6, I get:
**Socket.IO Version: undefined
有什么帮助吗?谢谢!
推荐答案
你可以这样做:
console.log("**Socket.IO Version: " + require('socket.io/package').version);
这个想法是加载 package.json
文件,其中包含有关 Node 包的信息.
The idea is to load package.json
file, which contains information about a Node package.
这是可能的,因为 Node 的 require
也能够加载 JSON 模块.
来自 文档:
This is possible because Node's require
is able to load JSON modules as well.
From the docs:
如果没有找到确切的文件名,那么 node 将尝试加载所需的文件名,添加扩展名为 .js
、.json
,然后是 .node
..js
文件被解释为 JavaScript 文本文件,而 .json
文件被解析为 JSON 文本文件 [...]
If the exact filename is not found, then node will attempt to load the required filename with the added extension of
.js
,.json
, and then.node
..js
files are interpreted as JavaScript text files, and.json
files are parsed as JSON text files [...]
这篇关于Socket.IO 版本输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!