bcrypt.hash throws 'data and salt arguments required'
Error: data and salt arguments requiredShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
The 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
The second value prints undefined, for example when it comes from a setting that was not loadedThe salt or rounds value is undefinedFix - 3
The 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
Error: data and hash arguments required when a user signs inbcrypt.compare throws the sibling error because the stored hash is missingFix
- 1
What does the error look like?
Error: data and salt arguments requiredError: 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
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" src2Fix
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
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/null2Fix
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
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" src2Fix
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 emailConfirmed by mongoosejs.com
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
Back end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
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.
3 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fixBack end
Browser blocks a stylesheet or script: MIME type mismatch with nosniff
The Network tab shows the asset URL answered with Content-Type text/html
With X-Content-Type-Options: nosniff the browser accepts a stylesheet only as text/css and a script only as a JavaScript type.
3 causes, each with a fixBack end
Discord OAuth2 login fails on the redirect_uri in an Express app
invalid_request
Discord compares the redirect_uri you send with the URL you registered for the application, and the token step must repeat the same value as the authorization step.
4 causes, each with a fix