ERR_ASSERTION
Node.jsERRORNotableAssertHIGH confidence

An assertion failed.

Production Risk

Low. The `assert` module is intended for use in tests, not production logic. An assertion failure should be caught by a test runner during development or CI/CD.

What this means

This error is thrown by the `assert` module when an assertion check fails. The `assert` module is used to write tests for your code by checking for invariants—conditions that should always be true. If an assertion fails, it means your code is not behaving as expected, and this error is thrown to halt execution and report the problem.

Why it happens
  1. 1A value in a test did not equal an expected value (`assert.strictEqual`).
  2. 2A block of code that was expected to throw an error did not (`assert.throws`).
  3. 3A condition that should have been truthy was falsy (`assert(myValue)`).
How to reproduce

This error occurs exclusively when a function from the `assert` module is called and its condition is not met.

trigger — this will error
trigger — this will error
const assert = require('assert');

try {
  // This assertion will fail because 1 does not equal 2.
  assert.strictEqual(1, 2, 'Values are not equal');
} catch (err) {
  console.error(err.code);
  // The error message will be 'Values are not equal'
}

expected output

ERR_ASSERTION

Fix 1

Fix the Failing Code

WHEN An assertion fails in a unit test.

Fix the Failing Code
// The code being tested:
function add(a, b) {
  return a + b; // Correct logic
}

// The test:
const assert = require('assert');
assert.strictEqual(add(1, 1), 2); // This will now pass.

Why this works

An assertion failure means there is a bug in the code being tested. The primary fix is to debug and correct the logic in your application code so that it produces the expected outcome.

Fix 2

Update the Assertion

WHEN The code's behavior has intentionally changed and the test is now outdated.

Update the Assertion
// The code's behavior has changed to return a string.
function add(a, b) {
  return String(a + b);
}

// Update the test to expect a string.
const assert = require('assert');
assert.strictEqual(add(1, 1), '2');

Why this works

If the underlying code's behavior has been deliberately changed, the test case must be updated to reflect the new, correct expectations.

Code examples
Triggerjs
const assert = require('assert');

try {
  // This assertion will fail because 1 does not equal 2.
  assert.strictEqual(1, 2, 'Values are not equal');
} catch (err) {  // this triggers ERR_ASSERTION
Handle in try/catchjs
try {
  // operation that may throw ERR_ASSERTION
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_ASSERTION') {
    console.error('ERR_ASSERTION:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Use assert only in tests; use explicit checks in production
function divide(a, b) {
  if (b === 0) throw new RangeError('Cannot divide by zero')
  return a / b
}
What not to do

Sources
Official documentation ↗

https://github.com/nodejs/node/blob/main/lib/internal/assert.js

More information

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors