【问题标题】:Check for current Node Version检查当前节点版本
【发布时间】:2011-07-11 20:45:32
【问题描述】:

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

【问题讨论】:

    标签: node.js


    【解决方案1】:

    尝试查看process.version 属性。

    【讨论】:

      【解决方案2】:

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

      如果process.version 是'v0.11.5',则得到0.11

      【讨论】:

      • node-semver 库对此非常有用。
      • 哦,是的,node-semver 是一个更好的解决方案
      • 突然这样 0.10 变成 0.1 > process.version 'v0.10.40' > Number(process.version.match(/^v(\d+\.\d+)/)[1]) 0.1
      • 不要使用这个,因为 Michael Plakhov 概述的原因:使用十进制数,0.10 == 0.1
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        使用semver比较process.version

        const semver = require('semver');
        
        if (semver.gte(process.version, '0.12.18')) {
          ...
        }
        

        【讨论】:

          【解决方案5】:

          如果你只需要检查主要版本,你可以使用这个又快又脏的 sn-p:

          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】:

            我稍微细化了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=$(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."
            }
            

            【讨论】:

              【解决方案7】:

              我的代码库也有类似的问题。我想知道我将在运行时用来运行我的服务器的当前 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 run start 命令运行项目之前,请运行npm install 命令。

              【讨论】:

                【解决方案8】:

                如果访问node js运行环境,主要有2个入口:(一个simeple,一个detail)

                • 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' }
                

                【讨论】:

                  【解决方案9】:

                  也不要像@alsotang 建议的那样写整个

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

                  (并不是说这是一个糟糕的解决方案)。

                  你可以简单地写

                  parseFloat(process.version<b>s</b>.node); 这是versions(复数)而不是version

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

                  【讨论】:

                  • 请勿使用此方法:正如其他 cmets 中所说,以这种方式解析会使版本以错误的方式读取:9.10 == 9.1
                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-05-30
                  • 1970-01-01
                  • 2015-04-24
                  • 2016-12-11
                  • 2021-11-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多