Registry / devops / if-node-version

if-node-version

JSON →
library1.1.1jsnpmunverified

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-version
INSTALL
IMPORT
SIG · IF-NODE-VERSION
I
if-node-version
devopsjavascriptv1.1.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

(Default Function Export)
const spawnIfNodeVersion = require('if-node-version');
import spawnIfNodeVersion from 'if-node-version';
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.
spawnIfNodeVersion.sync
const syncSpawn = require('if-node-version').sync;
import { sync } from 'if-node-version';
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.
(CLI Execution)
npx if-node-version ">=16" some-command --arg
node path/to/node_modules/if-node-version/bin/cli.js
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.

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.

# Install the package as a dev dependency npm install --save-dev if-node-version # Example 1: Run 'eslint' only if Node.js is version 16 or newer echo "--- Running ESLint (if Node.js >= 16) ---" npx if-node-version ">=16" echo "Running ESLint for Node.js >= 16..." echo "" # Example 2: Execute a fallback script if Node.js is older than 14 echo "--- Checking for Node.js < 14 and running fallback ---" # Create a dummy fallback script echo "console.log('Running fallback for older Node.js...');" > fallback.js npx if-node-version "<14" node fallback.js rm fallback.js # Clean up echo "" # Example 3: Just check the Node.js version and report its status echo "--- Checking if current Node.js is version 18 or newer ---" npx if-node-version ">=18" if [ $? -eq 0 ]; then echo "Current Node.js version is >= 18." else echo "Current Node.js version is NOT >= 18." fi
if-node-version --version
Debug
Known issues
gotchaThe `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.
fix
For projects supporting Node.js 0.10, ensure `npm install --save spawn-sync` is run if `spawnIfNodeVersion.sync` is used.
affects: 0.10.x
gotchaThe 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.
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.
affects: >=1.1.0
Errors
Common errors & fixes
'if-node-version' is not recognized as an internal or external command, operable program or batch file. OR if-node-version: command not found
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`.
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 ..." }`).
Error: Invalid Version: <your-version-range-string>
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.
fix
Review the `VersionRange` parameter against the `node-semver` documentation (e.g., `^12.0.0`, `>=14.x`, `<16`). Ensure the string is correctly formatted.
TypeError: Cannot read property 'sync' of undefined OR TypeError: (intermediate value).sync is not a function
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.
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');`.
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies
cross-spawnrequiredUsed internally for cross-platform process spawning to ensure commands run consistently across different operating systems.
spawn-syncoptionalRequired as a polyfill for `child_process.spawnSync` when using the `spawnIfNodeVersion.sync` API on Node.js versions 0.10.x.
Agent activity
4 hits · last 30 days
node
4
Resources
if-node-version — npm install if-node-version · libregistry