Check for current Node Version

Solution 1:

Try to look at process.version property.

Solution 2:

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

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

Solution 3:

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

Solution 4:

Use semver to compare process.version:

const semver = require('semver');

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

Solution 5:

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.node is 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".