Node.js Version Utility
node-version is a lightweight JavaScript utility designed to parse the current Node.js runtime version (from `process.version`) into a structured object. It provides granular access to major, minor, and patch segments, alongside powerful helper methods for semantic version comparison, such as `isAtLeast`, `isAbove`, `isBelow`, and `isAtMost`. The package, currently at stable version 4.3.0, maintains an active release cadence with minor and patch updates introducing features like `eolDate` and `daysUntilEOL`. Key differentiators include its pure ESM architecture (since v4), full TypeScript support, and built-in checks for Node.js Long Term Support (LTS) status and End-of-Life (EOL) dates, making it an ideal choice for modern Node.js applications that require robust version management and awareness.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module /path/to/node_modules/node-version/dist/index.js from /your/file.js not supported. Instead change the require of index.js to a dynamic import() or remove the 'type': 'module' in your package.json.
cause You are attempting to use `require()` to import the `node-version` package, which is an ES Module (ESM) since v4.0.0.fixChange your import statement from `const { version } = require('node-version');` to `import { version } from 'node-version';`. Ensure your project or file is configured for ESM (e.g., `"type": "module"` in `package.json` or `.mjs` file extension). If you cannot use ESM, install `node-version@3`. -
The engine "node" is incompatible with this module. Expected version ">=20.0.0". Got "18.17.0"
cause Your current Node.js runtime version does not meet the minimum requirement (`>=20.0.0`) specified by `node-version` v4+.fixUpgrade your Node.js environment to version 20.0.0 or newer. Alternatively, if your project must run on an older Node.js version, you can install `node-version@3` which supports earlier Node.js versions.
Warnings
- breaking Starting with v4.0.0, the `node-version` package is exclusively pure ES Module (ESM). CommonJS support has been entirely removed.
- breaking Version 4.0.0 and above explicitly require Node.js runtime version 20.0.0 or higher, as specified in the `engines` field.
- gotcha Attempting to use `require()` to import `node-version` in a CommonJS context will lead to an `ERR_REQUIRE_ESM` error.
- deprecated For applications requiring CommonJS support, version 3.x.x is the last major release that supports `require()`. All subsequent major versions are ESM-only.
Install
-
npm install node-version -
yarn add node-version -
pnpm add node-version
Imports
- version
const { version } = require('node-version');import { version } from 'node-version'; - getVersion
const getVersion = require('node-version').getVersion;import { getVersion } from 'node-version'; - NodeVersion
import type { NodeVersion } from 'node-version';
Quickstart
import { version, getVersion } from 'node-version';
// Access the pre-instantiated version object
console.log('Current Node.js version:', version.long);
console.log('Is LTS?', version.isLTS);
if (version.isLTS) {
console.log('LTS Name:', version.ltsName);
}
console.log('Is EOL?', version.isEOL);
if (version.isEOL) {
console.log('End-of-Life Date:', version.eolDate);
console.log('Days until EOL:', version.daysUntilEOL);
}
// Perform version comparisons
if (version.isAtLeast('20.0.0')) {
console.log('Running on Node.js 20 or newer.');
} else {
console.log('Running on an older Node.js version.');
}
if (version.isAbove('22.0.0')) {
console.log('Running on Node.js strictly above 22.0.0.');
}
// Get a new NodeVersion instance
const specificVersion = getVersion();
console.log('Another instance (same info):', specificVersion.original);