{"id":12834,"library":"assert-node-version","title":"Assert Node.js Version","description":"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.","status":"active","version":"1.0.3","language":"javascript","source_language":"en","source_url":"https://github.com/marcbachmann/assert-node-version","tags":["javascript","assert","version","node"],"install":[{"cmd":"npm install assert-node-version","lang":"bash","label":"npm"},{"cmd":"yarn add assert-node-version","lang":"bash","label":"yarn"},{"cmd":"pnpm add assert-node-version","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import assertNodeVersion from 'assert-node-version';","symbol":"assertNodeVersion","correct":"const assertNodeVersion = require('assert-node-version');"},{"note":"The module exports a function directly. It's often called immediately after requiring.","wrong":"const assert = require('assert-node-version').assert;","symbol":"directCall","correct":"require('assert-node-version')();"}],"quickstart":{"code":"const fs = require('fs');\nconst path = require('path');\n\n// Create a temporary package.json for demonstration\nconst tempDir = path.join(__dirname, 'temp-project');\nfs.mkdirSync(tempDir, { recursive: true });\nfs.writeFileSync(path.join(tempDir, 'package.json'), JSON.stringify({\n  \"name\": \"test-project\",\n  \"version\": \"1.0.0\",\n  \"engines\": { \"node\": \">=16.0.0\" }\n}, null, 2));\n\nconsole.log('--- Testing success case ---');\ntry {\n  // Pass the temporary directory to check its package.json\n  require('assert-node-version')(tempDir);\n  console.log(`Node.js version ${process.version} satisfies >=16.0.0 (as configured in temp-project/package.json).`);\n} catch (error) {\n  console.error('Unexpected error in success case:', error.message);\n}\n\n// Create a temporary .nvmrc for failure demonstration\nfs.writeFileSync(path.join(tempDir, '.nvmrc'), '12.0.0'); // Force an old version\n\nconsole.log('\\n--- Testing failure case ---');\ntry {\n  // Check against the .nvmrc in the temporary directory\n  require('assert-node-version')(tempDir);\n  console.log('This line should not be reached if assertion fails.');\n} catch (error) {\n  console.error(`Assertion failed as expected: ${error.message}`);\n}\n\n// Cleanup temporary files\nfs.unlinkSync(path.join(tempDir, 'package.json'));\nfs.unlinkSync(path.join(tempDir, '.nvmrc'));\nfs.rmdirSync(tempDir);\n","lang":"javascript","description":"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."},"warnings":[{"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); }`","message":"The 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.","severity":"breaking","affected_versions":">=0.1.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=0.1.0"},{"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, '../'))`.","message":"When 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.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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).","cause":"The installed Node.js version on the system does not meet the requirements specified in the project's `package.json` or `.nvmrc` file.","error":"Error: Current Node.js version X.Y.Z does not satisfy Y.Y.Y (defined in package.json)"},{"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.","cause":"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.","error":"TypeError: require(...) is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null}