Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

bcryptjs rejects hash() or compare() with Illegal arguments

ErrorError: Illegal arguments: undefined, string

Short answer

bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second. So undefined, string means the password is undefined. Read the pair: it tells you whether the password, the salt or the stored hash is the missing value.

  • Applies to bcryptjs 3, Node.js 22, Express 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the typeof of both values right before the bcrypt call. The error message is built from exactly these two typeof results, in the same order.

    console.log(typeof password, typeof saltOrHash);
    // hash(password, salt)       -> first = password, second = salt or rounds
    // compare(password, hash)    -> first = password, second = stored hash
  2. 2Match what you see, then jump to its fix

    1. 1Illegal arguments: undefined, string (or undefined, number) in a register or login routereq.body is empty because no body-parsing middleware ranFix
    2. 2Illegal arguments: string, undefinedThe stored hash is undefined when you call compare()Fix
    3. 3Illegal arguments: number, string, or object, stringThe password or salt is not a string or number of the right typeFix

What does the error look like?

  • Error: Illegal arguments: undefined, string
  • Error: Illegal arguments: string, undefined
  • Error: Illegal arguments: undefined, number

Cause 1 of 3

req.body is empty because no body-parsing middleware ran

1Check

In Express, req.body is undefined by default and is filled only when body-parsing middleware such as express.json() or express.urlencoded() runs before the route. Without it, req.body.password is undefined, or reading it throws first.

console.log(req.body);
console.log(req.headers['content-type']);

2Fix

Register the parsers before your routes. Use express.json() for JSON bodies and express.urlencoded() for HTML form posts.

const express = require('express');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// routes come after this

3Verify

Post a body again and confirm the log now shows the password field as a string.

curl -X POST http://localhost:3000/register -H 'Content-Type: application/json' -d '{"email":"a@example.com","password":"secret123"}'

Confirmed by expressjs.com

Did this fix it?

Cause 2 of 3

The stored hash is undefined when you call compare()

1Check

compare(password, hash) needs both as strings. The second one comes from your database row. It is undefined when no user was found, when the column name in code differs from the real one, or when the query excluded that field.

const user = await User.findOne({ email });
console.log(user && Object.keys(user.toObject ? user.toObject() : user));

2Fix

Stop early when there is no user, and use the field name your schema really has. Also make sure the query returns the hash column.

const user = await User.findOne({ email }).select('+passwordHash');
if (!user || typeof user.passwordHash !== 'string') {
  return res.status(401).json({ error: 'Invalid credentials' });
}
const ok = await bcrypt.compare(password, user.passwordHash);

3Verify

Log in with a real account and with an unknown email; the first returns a result of true or false, the second a 401, and neither throws.

curl -X POST http://localhost:3000/login -H 'Content-Type: application/json' -d '{"email":"nobody@example.com","password":"x"}'

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

The password or salt is not a string or number of the right type

1Check

In bcryptjs, hash() accepts a string password with a string salt or a number of rounds, and compare() needs two strings. A numeric PIN read from JSON, or an object from a nested form field, fails that check.

console.log(typeof req.body.password, req.body.password);

2Fix

Reject non-string passwords at the edge, or convert an intentionally numeric value with String() before hashing. Always use the same conversion when you compare.

const { password } = req.body ?? {};
if (typeof password !== 'string' || password.length === 0) {
  return res.status(400).json({ error: 'password is required' });
}
const hash = await bcrypt.hash(password, 10);

3Verify

Send a number and an object as password; both get a 400 and no stack trace.

curl -X POST http://localhost:3000/register -H 'Content-Type: application/json' -d '{"password":12345}'

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Validate the request body (types and presence) before any call into bcrypt.
  • Wrap login in one function that returns early when the user or the hash is missing.
  • Keep the column name for the hash in one place, such as the model, and do not copy it as a string around the code.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.