als*_*ang 26
Number(process.version.match(/^v(\d+\.\d+)/)[1])
如果process.version
是'v0.11.5',那么得到0.11
(数字).
Chr*_*use 23
使用semver进行比较process.version
:
const semver = require('semver');
if (semver.gte(process.version, '0.12.18')) {
...
}
Run Code Online (Sandbox Code Playgroud)
And*_*ega 20
实际上,最好使用process.versions
为不同节点组件提供大量版本的对象.例:
{
http_parser: '2.5.2',
node: '4.4.3',
v8: '4.5.103.35',
uv: '1.8.0',
zlib: '1.2.8',
ares: '1.10.1-DEV',
icu: '56.1',
modules: '46',
openssl: '1.0.2g'
}
Run Code Online (Sandbox Code Playgroud)
如果你只需要检查主要版本,你可以使用这个快速而肮脏的片段:
const NODE_MAJOR_VERSION = process.versions.node.split('.')[0];
if (NODE_MAJOR_VERSION < 12) {
throw new Error('Requires Node 12 (or higher)');
}
Run Code Online (Sandbox Code Playgroud)
笔记:
process.versions.node
比 更容易使用process.version
,因为您不必担心版本是否以前导v
."0"
。 归档时间: |
|
查看次数: |
61774 次 |
最近记录: |