Check out the current version of Node

I need to programmatically access the current version of node running in the library I'm writing. Seems unable to find this in the docs.

+115
Jul 11 2018-11-11T00:
source share
6 answers

Try looking at the process.version property.

+184
Jul 11 2018-11-11T00:
source share

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

if process.version is 'v0.11.5', then get 0.11 (Number).

+26
Dec 27 '13 at 10:11
source share

Use semver to compare process.version :

 const semver = require('semver'); if (semver.gte(process.version, '0.12.18')) { ... } 
+22
Mar 01 '17 at 15:51
source share

In fact, it would be better to use the process.versions object, which provides many versions for the various components of the node. Example:

 { 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' } 
+19
Jun 21 '16 at 9:15
source share

I had a similar problem with my codebase. I wanted to know the current version of NodeJ that I was going to use to start my server at runtime. To do this, I wrote code that can be run before starting the Server using the npm run start script. Below is the code useful for this 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}.'); } 

My package.json looks like this:

 { "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 command before npm run start command to start the project.

+2
Jun 07 '18 at 17:07
source share

If you are accessing a js node running in an environment, there are 2 main entries: (one example, one part)

  • process.version will give you:

'v10.16.0'

  • process.versions will give you:
 { 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' } 
+1
Jul 03 '19 at 0:32
source share



All Articles