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.
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
."0"
.