If-node-version is a focused utility designed to conditionally execute shell commands or check the current Node.js runtime version against specified semver ranges. It is primarily intended for use in `npm-scripts` or CI/CD environments where different Node.js versions might require varied build or test processes. The package exposes both a command-line interface (CLI) for direct shell scripting and a Node.js API that leverages `child_process.spawn` and `child_process.spawnSync` for programmatic control. The current stable version is 1.1.1. While the project has a slower release cadence, with the last update (v1.1.1) in May 2017 (following v1.1.0 in Feb 2017 and v1.0.0 in Nov 2016), it remains active for its intended purpose. Its key differentiator lies in its simple, `semver`-compliant approach to conditional execution, avoiding more complex environment variable checks or custom script logic.
npm install if-node-versionVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to install `if-node-version` and use its CLI to conditionally execute commands or check the Node.js version, including examples for `npm-scripts` and exit code handling.
For projects supporting Node.js 0.10, ensure `npm install --save spawn-sync` is run if `spawnIfNodeVersion.sync` is used.
Always explicitly check the intended exit code behavior in your scripts. For simple version checks, rely on the 0/1 exit code. For conditional command execution, be aware that a skipped command will lead to a 0 exit code from `if-node-version` itself.
Ensure `if-node-version` is run through `npx` (e.g., `npx if-node-version ...`) or defined within your `package.json` scripts (e.g., `"scripts": { "check": "if-node-version ..." }`).Review the `VersionRange` parameter against the `node-semver` documentation (e.g., `^12.0.0`, `>=14.x`, `<16`). Ensure the string is correctly formatted.
If on Node.js 0.10.x and using `.sync()`, install `spawn-sync`: `npm install --save spawn-sync`. Also, ensure `if-node-version` is correctly `require()`d as a CommonJS module, e.g., `const ifNodeVersion = require('if-node-version');`.