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.
10 Answers
process.version.match(/^v(\d+\.\d+)/)[1]
if process.version is 'v0.11.5', then get 0.11 .
 
    
    - 1,520
- 1
- 13
- 15
- 
                    7The [node-semver](https://github.com/npm/node-semver) library can be very useful for this. – beeman Aug 23 '15 at 00:51
- 
                    2oh yes, `node-semver` is a better sollution – alsotang Aug 27 '15 at 05:34
- 
                    3Suddenly this way 0.10 become 0.1 > process.version 'v0.10.40' > Number(process.version.match(/^v(\d+\.\d+)/)[1]) 0.1 – Michael Plakhov Sep 04 '15 at 08:31
- 
                    2DO NOT USE THIS for the reason outlined by Michael Plakhov: using decimal numbers, 0.10 == 0.1 – caesarsol May 27 '20 at 10:40
Actually it would be better to use process.versions object which provides a lot of versions for the different node components.
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'
}
 
    
    - 2,561
- 4
- 25
- 71
 
    
    - 27,211
- 13
- 45
- 46
Use semver to compare process.version:
const semver = require('semver');
if (semver.gte(process.version, '0.12.18')) {
  ...
}
 
    
    - 5,090
- 1
- 32
- 28
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 than- process.version, as you do not have to worry about whether the version starts with a leading- v.
- 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".
 
    
    - 41,306
- 31
- 146
- 239
I refined alsotang's answer a bit to compare versions:
const m = process.version.match(/(\d+)\.(\d+)\.(\d+)/);
const [major, minor, patch] = m.slice(1).map(_ => parseInt(_));
To perform an assertion, do it like this:
if (major >= 13 || (major >= 12 && minor >= 12)) {
    console.log("NodeJS is at least v12.12.0. It is safe to use fs.opendir!");
}
This can be shortened to a one-liner to use in 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
or 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."
}
 
    
    - 379
- 2
- 7
If you access node js running environments, there are 2 main entries: (one simeple, one detail)
- process.versionwill give you:
'v10.16.0'
- process.versionswill 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' }
 
    
    - 33,823
- 14
- 84
- 85
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 start script.
Found below code helpful from 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"
  }
}
Do run npm install command before you run npm run start command to run your project.
 
    
    - 3,394
- 28
- 20
also instead of writing this whole as suggested by @alsotang
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
to get same or (similar) result and is easy to read
Note: only if you know that minor version is not going to be greater than 9, as pointed out be caesarsol in comments.
 
    
    - 804
- 1
- 6
- 21
- 
                    1DO NOT USE THIS METHOD: as said in other comments, parsing in this way makes the version read in a wrong way: 9.10 == 9.1 – caesarsol May 27 '20 at 10:42
How about this, for major, minor
const [NODE_MAJOR_VERSION, NODE_MINOR_VERSION] = process.versions.node.split('.')
 
    
    - 11
- 1
- 1
 
     
    