Maaz Zindani

Loading the index…

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

bcrypt.hash throws 'data and salt arguments required'

ErrorError: data and salt arguments required

Short answer

The bcrypt package throws this when hash gets null or undefined for the password or for the salt. Log both values just before the call to see which one is missing. The usual causes are a request body that was never parsed, a rounds value that never loaded, and a Mongoose save hook that hashes a password the document does not hold.

  • Applies to bcrypt for Node.js, Node.js 22, Express 5, Mongoose
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the type of each argument right before the call. The one that prints undefined is the missing one.

    console.log({ password: typeof password, rounds: typeof saltRounds });
    const hashed = await bcrypt.hash(password, saltRounds);
  2. 2Match what you see, then jump to its fix

    1. 1The first value prints undefined, and the request itself looks correct in the clientThe password never reaches the handler because the body was not parsedFix
    2. 2The second value prints undefined, for example when it comes from a setting that was not loadedThe salt or rounds value is undefinedFix
    3. 3The error comes from an update of an existing user, while sign-up worksA Mongoose pre-save hook hashes a password the document does not holdFix
    4. 4Error: data and hash arguments required when a user signs inbcrypt.compare throws the sibling error because the stored hash is missingFix

What does the error look like?

  • Error: data and salt arguments required
  • Error: data and hash arguments required

Cause 1 of 4

The password never reaches the handler because the body was not parsed

1Check

In Express, req.body is undefined until body-parsing middleware fills it. express.json() parses application/json and express.urlencoded() parses form posts, each only for its own content type. A body in a format you did not register, or a field name that differs from the one you read, leaves the password undefined. If req.body itself is undefined, reading req.body.password throws a TypeError first, so this cause shows when the body is an object that lacks the key.

app.post('/register', (req, res) => {
  console.log(req.headers['content-type'], Object.keys(req.body ?? {}));
  res.sendStatus(204);
});

2Fix

Register the parsers before your routes, send the content type your parser expects, and read the field under the name the client sends.

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.post('/register', async (req, res) => {
  const hashed = await bcrypt.hash(req.body.password, 10);
  res.json({ ok: Boolean(hashed) });
});

3Verify

Post the form again and confirm the log lists the password key, then that hash resolves.

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

Confirmed by expressjs.com

Did this fix it?

Cause 2 of 4

The salt or rounds value is undefined

1Check

The second argument of hash is required. The README says it can be a number of rounds, and a salt is generated from it, or a salt you made yourself. A missing configuration value leaves it undefined.

grep -rn "bcrypt.hash\|bcrypt.hashSync" src

2Fix

Pass a number, with a default so the call never gets undefined.

const saltRounds = Number(process.env.SALT_ROUNDS) || 10;
const hashed = await bcrypt.hash(password, saltRounds);

3Verify

The log line from the first step shows number for rounds, and hash resolves.

node -e "require('bcrypt').hash('example-password', 10).then(console.log)"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 4

A Mongoose pre-save hook hashes a password the document does not hold

1Check

A schema path with select: false is left out of query results, so a loaded document has no password to hash. A pre-save hook that hashes this.password on every save then passes undefined.

grep -rn "pre('save'\|select: false" src models 2>/dev/null

2Fix

Hash only when the password was changed in this document.

userSchema.pre('save', async function () {
  if (!this.isModified('password')) return;
  this.password = await bcrypt.hash(this.password, 10);
});

3Verify

Update a user's name without touching the password; the save succeeds without calling hash.

const user = await User.findOne({ email });
user.name = 'New name';
await user.save();

Confirmed by mongoosejs.com

Did this fix it?

Cause 4 of 4

bcrypt.compare throws the sibling error because the stored hash is missing

1Check

compare needs both the plain password and the stored hash. The hash is undefined when no user was found, or when the query left out a password path marked select: false.

grep -rn "bcrypt.compare" src

2Fix

Return early when there is no user, and select the hash explicitly when the schema hides it.

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

3Verify

A valid sign-in returns ok as true, and an unknown email returns 401 without an exception.

# repeat the sign-in request for a known and an unknown email

Confirmed by mongoosejs.com

Did this fix it?

How do I stop it coming back?

  • Validate the request body before hashing: reject a missing or empty password with a 400, so bcrypt never sees undefined.
  • Keep the rounds value in one constant that has a default, not in several env reads.
  • Guard every pre-save hook with isModified for the field it changes.
  • Add a test that posts a body with no password and expects a 400.

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.