node.js 检查当前节点版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6656324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check for current Node Version
提问by Harborhoffer
I need to programmatically access the current node version running in a library I am writing. Can't seem to find this in the docs.
我需要以编程方式访问我正在编写的库中运行的当前节点版本。似乎在文档中找不到这个。
回答by yojimbo87
Try to look at process.versionproperty.
尝试查看process.version属性。
回答by alsotang
process.version.match(/^v(\d+\.\d+)/)[1]
process.version.match(/^v(\d+\.\d+)/)[1]
if process.versionis 'v0.11.5', then get 0.11.
如果process.version是'v0.11.5',那么得到0.11。
回答by Christian d'Heureuse
回答by Andrea Baccega
Actually it would be better to use process.versionsobject which provides a lot of versions for the different node components.
Example:
实际上,最好使用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'
}
回答by Philipp Cla?en
If you need to only check for the major version, you can use this quick-and-dirty snippet:
如果你只需要检查主要版本,你可以使用这个快速而肮脏的片段:
const NODE_MAJOR_VERSION = process.versions.node.split('.')[0];
if (NODE_MAJOR_VERSION < 12) {
throw new Error('Requires Node 12 (or higher)');
}
Notes:
笔记:
process.versions.nodeis easier to work with thanprocess.version, as you do not have to worry about whether the version starts with a leadingv.- If you still need to distinguish between ancient versions (e.g., 0.10 and 0.12), this will not work, as they will all be considered version
"0".
process.versions.node比 更容易使用process.version,因为您不必担心版本是否以前导v.- 如果您仍然需要区分古代版本(例如 0.10 和 0.12),这将不起作用,因为它们都将被视为 version
"0"。
回答by S.Mishra
I had the similar issue with my codebase. I wanted to know the current NodeJs version I am going to use to run my server at runtime. For that, I wrote a code which can be run before starting the Server using npm run startscript.
Found below code helpful from this question.
我的代码库也有类似的问题。我想知道我将用来在运行时运行我的服务器的当前 NodeJs 版本。为此,我编写了一个可以在使用npm run start脚本启动服务器之前运行的代码。发现下面的代码对这个问题有帮助。
'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}.`);
}
My package.json looks like this:
我的 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"
}
}
Do run npm installcommand before you run npm run startcommand to run your project.
不要运行npm install之前运行命令npm run start命令运行项目。
回答by Xin
If you access node js running environments, there are 2 main entries: (one simeple, one detail)
如果访问node js运行环境,主要有2个入口:(一个简单,一个细节)
process.versionwill give you:
process.version会给你:
'v10.16.0'
'v10.16.0'
process.versionswill give you:
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' }
回答by sanjeev
also instead of writing this whole as suggested by @alsotang
也不要像@alsotang 建议的那样写整个
Number(process.version.match(/^v(\d+\.\d+)/)[1])
Number(process.version.match(/^v(\d+\.\d+)/)[1])
(not saying this is a bad solution).
(并不是说这是一个糟糕的解决方案)。
you can simply write
你可以简单地写
parseFloat(process.versions.node);it is versions(plural) not version
parseFloat(process.versions.node);它是版本(复数)不是版本
to get same or (similar) result and is easy to read
获得相同或(相似)结果并且易于阅读

