{"id":13320,"library":"if-node-version","title":"Conditional Node.js Version Execution","description":"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.","status":"active","version":"1.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/mysticatea/if-node-version","tags":["javascript","node","version","semver","range","check","compare","conditional","less"],"install":[{"cmd":"npm install if-node-version","lang":"bash","label":"npm"},{"cmd":"yarn add if-node-version","lang":"bash","label":"yarn"},{"cmd":"pnpm add if-node-version","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used internally for cross-platform process spawning to ensure commands run consistently across different operating systems.","package":"cross-spawn","optional":false},{"reason":"Required as a polyfill for `child_process.spawnSync` when using the `spawnIfNodeVersion.sync` API on Node.js versions 0.10.x.","package":"spawn-sync","optional":true}],"imports":[{"note":"This package is CommonJS-only. Its primary export is a function for asynchronous process spawning. Attempting ES module `import` syntax will result in errors in environments configured for CommonJS.","wrong":"import spawnIfNodeVersion from 'if-node-version';","symbol":"(Default Function Export)","correct":"const spawnIfNodeVersion = require('if-node-version');"},{"note":"The synchronous `spawn` function is a property attached to the main exported function. Accessing it via ES module named imports is incorrect for a CommonJS package. It also requires `spawn-sync` as a peer dependency for Node.js 0.10.","wrong":"import { sync } from 'if-node-version';","symbol":"spawnIfNodeVersion.sync","correct":"const syncSpawn = require('if-node-version').sync;"},{"note":"The most common and recommended way to use `if-node-version` is via its command-line interface, typically invoked using `npx` or directly from `package.json` scripts, rather than directly executing the binary or importing its internal CLI module.","wrong":"node path/to/node_modules/if-node-version/bin/cli.js","symbol":"(CLI Execution)","correct":"npx if-node-version \">=16\" some-command --arg"}],"quickstart":{"code":"# Install the package as a dev dependency\nnpm install --save-dev if-node-version\n\n# Example 1: Run 'eslint' only if Node.js is version 16 or newer\necho \"--- Running ESLint (if Node.js >= 16) ---\"\nnpx if-node-version \">=16\" echo \"Running ESLint for Node.js >= 16...\"\n\necho \"\"\n\n# Example 2: Execute a fallback script if Node.js is older than 14\necho \"--- Checking for Node.js < 14 and running fallback ---\"\n# Create a dummy fallback script\necho \"console.log('Running fallback for older Node.js...');\" > fallback.js\nnpx if-node-version \"<14\" node fallback.js\nrm fallback.js # Clean up\n\necho \"\"\n\n# Example 3: Just check the Node.js version and report its status\necho \"--- Checking if current Node.js is version 18 or newer ---\"\nnpx if-node-version \">=18\"\nif [ $? -eq 0 ]; then\n  echo \"Current Node.js version is >= 18.\"\nelse\n  echo \"Current Node.js version is NOT >= 18.\"\nfi","lang":"bash","description":"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."},"warnings":[{"fix":"For projects supporting Node.js 0.10, ensure `npm install --save spawn-sync` is run if `spawnIfNodeVersion.sync` is used.","message":"The `spawnIfNodeVersion.sync` function requires the `spawn-sync` package to be explicitly installed as a peer dependency if targeting Node.js version 0.10.x. Without it, synchronous operations may fail or throw errors on this specific older Node.js runtime.","severity":"gotcha","affected_versions":"0.10.x"},{"fix":"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.","message":"The CLI's exit code behavior differs based on its usage. When `if-node-version <VersionRange> <Command> [...args]` is used, the exit code is that of the `<Command>` if it runs, or 0 if the version range is not satisfied and the command is skipped. However, when used as `if-node-version <VersionRange>` for a simple check, it exits with 0 if the version matches and 1 otherwise. This distinction is crucial for robust shell scripting.","severity":"gotcha","affected_versions":">=1.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"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 ...\" }`).","cause":"The `if-node-version` executable is not in the system's PATH. This commonly occurs when installed locally and not run via `npm scripts` or `npx`.","error":"'if-node-version' is not recognized as an internal or external command, operable program or batch file.\nOR\nif-node-version: command not found"},{"fix":"Review the `VersionRange` parameter against the `node-semver` documentation (e.g., `^12.0.0`, `>=14.x`, `<16`). Ensure the string is correctly formatted.","cause":"The provided version range string does not conform to the `node-semver` specification. This could be due to typos, invalid operators, or malformed version numbers.","error":"Error: Invalid Version: <your-version-range-string>"},{"fix":"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');`.","cause":"This typically occurs when trying to use `spawnIfNodeVersion.sync` on Node.js 0.10.x without having `spawn-sync` installed, or if the module itself was imported incorrectly.","error":"TypeError: Cannot read property 'sync' of undefined\nOR\nTypeError: (intermediate value).sync is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"if-node-version","cli_version":null}