检查当前的节点版本

时间:2011-07-11 20:45:32

标签: node.js

我需要以编程方式访问我正在编写的库中运行的当前节点版本。似乎无法在文档中找到这个。

9 个答案:

答案 0 :(得分:171)

尝试查看process.version属性。

答案 1 :(得分:25)

Number(process.version.match(/^v(\d+\.\d+)/)[1])

如果process.version为'v0.11.5',则获取0.11(数字)。

答案 2 :(得分:18)

实际上最好使用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'
}

答案 3 :(得分:18)

使用semver来比较process.version

const semver = require('semver');

if (semver.gte(process.version, '0.12.18')) {
  ...
}

答案 4 :(得分:2)

我的代码库遇到了类似的问题。我想知道我将用于在运行时运行我的服务器的当前NodeJs版本。为此,我编写了一个代码,可以在使用npm run start脚本启动服务器之前运行。 在以下代码中找到了有用的question

'use strict';
const semver = require('semver');
const engines = require('./package').engines;
const nodeVersion = engines.node;

// Compare installed NodeJs version with required NodeJs version.
if (!semver.satisfies(process.version, nodeVersion)) {
  console.log(`NodeJS Version Check: Required node version ${nodeVersion} NOT SATISFIED with current version ${process.version}.`);
  process.exit(1);
} else {
  console.log(`NodeJS Version Check: Required node version ${nodeVersion} SATISFIED with current version ${process.version}.`);
}

我的package.json看起来像这样:

{
  "name": "echo-server",
  "version": "1.0.0",
  "engines": {
    "node": "8.5.0",
    "npm": "5.3.0"
  },
  "description": "",
  "main": "index.js",
  "scripts": {
    "check-version" : "node checkVersion.js",
    "start-server" : "node app.js"
    "start" : "npm run check-version && npm run start-server",
    "test": "npm run check-version && echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bluebird": "^3.5.1",
    "express": "^4.16.3",
    "good-guy-http": "^1.10.3",
    "semver": "^5.5.0"
  }
}

在运行npm install命令运行项目之前,请运行npm run start命令。

答案 5 :(得分:2)

如果您只需要检查主要版本,则可以使用以下快捷代码段:

const NODE_MAJOR_VERSION = process.versions.node.split('.')[0];
if (NODE_MAJOR_VERSION < 12) {
  throw new Error('Requires Node 12 (or higher)');
}

注意:

  • process.versions.nodeprocess.version更易于使用,因为您不必担心版本是否以前导v开头。
  • 如果您仍然需要区分古代版本(例如0.10和0.12),则此方法将无效,因为它们都将被视为版本"0"

答案 6 :(得分:0)

如果您访问节点js运行环境,则有2条主要条目:(一项,详细信息)

  • process.version将为您提供:

'v10.16.0'

  • process.versions将为您提供:
{ http_parser: '2.8.0',
  node: '10.16.0',
  v8: '6.8.275.32-node.52',
  uv: '1.28.0',
  zlib: '1.2.11',
  brotli: '1.0.7',
  ares: '1.15.0',
  modules: '64',
  nghttp2: '1.34.0',
  napi: '4',
  openssl: '1.1.1b',
  icu: '64.2',
  unicode: '12.1',
  cldr: '35.1',
  tz: '2019a' }

答案 7 :(得分:0)

也不要像@alsotang所建议的那样编写整个内容

Number(process.version.match(/^v(\d+\.\d+)/)[1])

(不是说这是一个不好的解决方案)。

您只需编写

parseFloat(process.versions.node); 它是版本(复数)而不是版本

获得相同或相似的结果,并且易于阅读

答案 8 :(得分:0)

我还细化了Alsotang的答案以比较版本:

const m = process.version.match(/(\d+)\.(\d+)\.(\d+)/);
const [major, minor, patch] = m.slice(1).map(_ => parseInt(_));

要执行断言,请执行以下操作:

if (major >= 13 || (major >= 12 && minor >= 12)) {
    console.log("NodeJS is at least v12.12.0. It is safe to use fs.opendir!");
}

这可以简化为在bash中使用的单线:

NODE_VERSION_CHECK=$(node -e "const v = process.version.match(/(\\d+)\.(\\d+)\.(\\d+)/).slice(1).map(_ => parseInt(_)); console.log(v[0] >= 13 || (v[0] >= 12 && v[1] >= 12))")
if $NODE_VERSION -eq "true" ;
then
    echo "NodeJS is at least v12.12.0."
fi

或PowerShell:

$nodeVersion = $(node -e "const v = process.version.match(/(\d+)\.(\d+)\.(\d+)/).slice(1).map(_ => parseInt(_)); console.log(v[0] >= 13 || (v[0] >= 12 && v[1] >= 12))")
if ($nodeVersion -eq "true") {
    Write-Host "NodeJS is at least v12.12.0."
}
相关问题