A TypeScript library for checking system dependency versions. Verify that required command-line tools exist and meet minimum version requirements in your Node.js projects.
<, >, =, <=, >= for version comparisonsnpm install requires-version --save-dev
Check if a command-line tool is installed:
import { requires } from 'requires-version'
// Check if Node.js is installed
if (requires('node')) {
console.log('Node.js is installed')
} else {
console.log('Node.js is not installed')
}
Verify that a tool meets version requirements:
import { requires, GREATER, EQUAL } from 'requires-version'
// Check if Node.js version is >= 18.0.0
if (requires('node', '18.0.0', GREATER | EQUAL)) {
console.log('Node.js version is 18.0.0 or higher')
}
// Check if npm version is exactly 9.5.0
if (requires('npm', '9.5.0', EQUAL)) {
console.log('npm version is 9.5.0')
}
import { requires, LESS, GREATER, EQUAL } from 'requires-version'
// Greater than
requires('node', '16.0.0', GREATER) // version > 16.0.0
// Less than
requires('node', '20.0.0', LESS) // version < 20.0.0
// Equal to
requires('node', '18.14.1', EQUAL) // version === 18.14.1
// Greater than or equal to
requires('node', '18.0.0', GREATER | EQUAL) // version >= 18.0.0
// Less than or equal to
requires('node', '20.0.0', LESS | EQUAL) // version <= 20.0.0
When a version check fails, a VersionException is thrown:
import { requires, GREATER, EQUAL, VersionException } from 'requires-version'
try {
requires('nonexistent-tool', '1.0.0', GREATER | EQUAL)
} catch (error) {
if (error instanceof VersionException) {
console.error('Version check failed:', error.message)
// Possible messages:
// - "nonexistent-tool" not found.
// - "nonexistent-tool" version not found.
}
}
Validate your development environment before running tasks:
import { requires, GREATER, EQUAL } from 'requires-version'
function checkDevelopmentEnvironment() {
const checks = [
{ name: 'node', version: '18.0.0' },
{ name: 'npm', version: '9.0.0' },
{ name: 'git', version: '2.0.0' }
]
for (const { name, version } of checks) {
if (!requires(name, version, GREATER | EQUAL)) {
throw new Error(`${name} >= ${version} is required`)
}
}
console.log('โ
All dependencies satisfied')
}
import { requires, GREATER, EQUAL } from 'requires-version'
// Ensure required build tools are available
if (!requires('docker')) {
throw new Error('Docker is required for building')
}
if (!requires('node', '18.0.0', GREATER | EQUAL)) {
throw new Error('Node.js 18.0.0 or higher is required')
}
// Proceed with build...
requires(dependencyName: string): booleanCheck if a command exists in the system PATH.
Parameters:
dependencyName - Name of the command-line tool to checkReturns:
true if the tool exists, false otherwiseExample:
requires('node') // true or false
requires(dependencyName: string, version: string, op: Operator): booleanCheck if a command exists and satisfies a version requirement.
Parameters:
dependencyName - Name of the command-line tool to checkversion - Version string to compare against (e.g., "18.14.1")op - Comparison operator (LESS, GREATER, EQUAL, or combinations)Returns:
true if the version requirement is satisfiedThrows:
VersionException if the tool is not found or version cannot be determinedExample:
requires('node', '18.0.0', GREATER | EQUAL) // true or false
export const LESS: number // 0x02 - Less than operator
export const GREATER: number // 0x04 - Greater than operator
export const EQUAL: number // 0x08 - Equal to operator
Operators can be combined using bitwise OR (|):
GREATER | EQUAL โ >=LESS | EQUAL โ <=VersionExceptionCustom error class thrown when version checks fail.
Example:
import { VersionException } from 'requires-version'
try {
requires('tool', '1.0.0', EQUAL)
} catch (error) {
if (error instanceof VersionException) {
console.error(error.message)
}
}
which package to locate executables in the system PATH-v, --version, etc.)compare-versions packageTested on:
# Install dependencies
npm install
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run coverage
# Build
npm run build
# Lint
npm run lint
# Lint and fix
npm run lint:fix
The library includes comprehensive tests covering:
See test/index.spec.ts for all test cases.
Contributions are welcome! Please read our Contributing Guide for details on:
MIT ยฉ sanghaklee
which