How to determine Node.JS version from code

We look at a simple way to determine the Node.JS version from code to ensure all your requirements are met.


With all the new Node.JS features available sometimes you need to check the version being used to ensure that the minimum requirements are met.

This can be especially helpful with command line scripts. Early on by checking the version you can inform the user if they aren’t meeting the requirements and provide upgrade information or instructions.

The easiest way to check the Node.JS version from code is to use the process.version variable which exposes the version in the form of v8.2.1. The other variable available is process.versions.node which is pretty much the same but omitting the v and just contains the pure numeric version.

As the most common use case for checking the Node.JS version is to compare it to your minimum requirements, using the semver package is the simplest, best method.

const semver = require('semver');

const nodeVersion = process.versions.node;

console.log('Node.JS Version:', nodeVersion);

// use GTE, do we have atleast 6.17.1
const gteSatisfied = semver.gte('6.17.1', nodeVersion);
console.log('GTE is satisfied?', gteSatisfied);

// we can pass multiple conditions to satisfies
// do we a) have 6.17.1 or b) 8.0.0 or higher
const satisfies = semver.satisfies(nodeVersion, '=6.17.1 || >= 8.0.0');
console.log('Satisfies?', satisfies);

That’s all there is to it. Checking the minimum version requirements early on can enhance the user experience.

Often without this information, the user is left to go hunting through Stack Overflow or GitHub issues to solve problems when things don’t work as expected.