Registry / devops / assert-node-version

assert-node-version

JSON →
library1.0.3jsnpmunverified

assert-node-version is a lightweight utility designed to programmatically verify the current Node.js runtime version against a specified requirement, typically defined in a project's `package.json` file under the `engines.node` field or within a `.nvmrc` file. The current stable version is 1.0.3. It operates synchronously and will throw an error if the installed Node.js version does not meet the specified criteria, effectively halting execution early in scenarios where version compatibility is critical. This package offers a simple, direct API with minimal configuration, differentiating itself by its singular focus on version assertion and its reliance on common project configuration files rather than complex custom setup. Its release cadence appears to be stable and infrequent, reflecting its mature and focused functionality.

npm install assert-node-version
INSTALL
IMPORT
SIG · ASSERT-NODE-VERSIO
A
assert-node-version
devopsjavascriptv1.0.3
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.

assertNodeVersion
const assertNodeVersion = require('assert-node-version');
import assertNodeVersion from 'assert-node-version';
The package primarily uses a CommonJS default export. For ESM, you'd typically need a CommonJS interop wrapper or configure your build system appropriately. The default export is a function that can be called directly.
directCall
require('assert-node-version')();
const assert = require('assert-node-version').assert;
The module exports a function directly. It's often called immediately after requiring.

This quickstart demonstrates how to use `assert-node-version` by creating temporary project configurations (`package.json` and `.nvmrc`) to simulate both successful version checks and cases where the assertion fails, leading to an error being thrown.

const fs = require('fs'); const path = require('path'); // Create a temporary package.json for demonstration const tempDir = path.join(__dirname, 'temp-project'); fs.mkdirSync(tempDir, { recursive: true }); fs.writeFileSync(path.join(tempDir, 'package.json'), JSON.stringify({ "name": "test-project", "version": "1.0.0", "engines": { "node": ">=16.0.0" } }, null, 2)); console.log('--- Testing success case ---'); try { // Pass the temporary directory to check its package.json require('assert-node-version')(tempDir); console.log(`Node.js version ${process.version} satisfies >=16.0.0 (as configured in temp-project/package.json).`); } catch (error) { console.error('Unexpected error in success case:', error.message); } // Create a temporary .nvmrc for failure demonstration fs.writeFileSync(path.join(tempDir, '.nvmrc'), '12.0.0'); // Force an old version console.log('\n--- Testing failure case ---'); try { // Check against the .nvmrc in the temporary directory require('assert-node-version')(tempDir); console.log('This line should not be reached if assertion fails.'); } catch (error) { console.error(`Assertion failed as expected: ${error.message}`); } // Cleanup temporary files fs.unlinkSync(path.join(tempDir, 'package.json')); fs.unlinkSync(path.join(tempDir, '.nvmrc')); fs.rmdirSync(tempDir);
Debug
Known issues
breakingThe module throws an error and halts execution if the current Node.js version does not satisfy the specified requirement. This is its intended behavior but can be unexpected if not handled with a `try...catch` block or understood as an early exit mechanism.
fix
Wrap calls to `assert-node-version` in a `try...catch` block if you need to gracefully handle version mismatches rather than letting the process exit. Example: `try { require('assert-node-version')(); } catch (e) { console.error('Version mismatch:', e.message); process.exit(1); }`
affects: >=0.1.0
gotchaThe package reads version requirements from either `package.json`'s `engines.node` field or an `.nvmrc` file. If both are present, the `.nvmrc` file typically takes precedence. Ensure your version specification (e.g., `>=16.0.0` or `18.x`) is correctly formatted and accurately reflects your project's needs.
fix
Verify the contents of `package.json` (specifically `engines.node`) and `.nvmrc` files in the directory where the assertion is run. Avoid conflicting specifications between these two files for clarity.
affects: >=0.1.0
gotchaWhen called without an argument, `assert-node-version` defaults to using `process.cwd()` to find `package.json` or `.nvmrc`. Running the assertion from an unexpected working directory can lead to incorrect version checks against the wrong project configuration.
fix
Always ensure the Node.js process's current working directory (`process.cwd()`) is the root of the project you intend to check, or explicitly pass the correct project directory path to the function, e.g., `require('assert-node-version')(path.join(__dirname, '../'))`.
affects: >=0.1.0
Errors
Common errors & fixes
Error: Current Node.js version X.Y.Z does not satisfy Y.Y.Y (defined in package.json)
The installed Node.js version on the system does not meet the requirements specified in the project's `package.json` or `.nvmrc` file.
fix
Update your Node.js installation to a version that satisfies the requirement (e.g., using `nvm install <version>` and `nvm use <version>`), or adjust the version requirement in your `package.json` or `.nvmrc` file to match your current Node.js version (if appropriate).
TypeError: require(...) is not a function
This error occurs if you try to destructure or access properties on the result of `require('assert-node-version')`, but the module exports a function directly, not an object.
fix
Call the required module directly as a function: `require('assert-node-version')(directory)` instead of `const assert = require('assert-node-version').assert;` or similar.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
Amazon
1
OpenAI (training)
1
Resources
assert-node-version — npm install assert-node-version · libregistry