This document describes the patches and fork used for whatsapp-web.js to fix critical bugs that are not yet resolved in the official repository.
⚠️ IMPORTANT: These are temporary fixes and should be removed once the official whatsapp-web.js repository releases versions that include these fixes.
Instead of using the official package with patches, we're using a fork that already includes the duplicate events fix.
Why: The duplicate events patch caused issues with session restoration, preventing the READY event from firing and blocking message reception.
This patch file is applied on top of timothydillan's fork to add the markedUnread fix.
When sending messages, the following error occurs:
Cannot read properties of undefined (reading 'markedUnread')
This happens because WhatsApp Web's internal structure changed, and the chat.markedUnread property may not exist in certain chat types (channels, status broadcasts, or newer WhatsApp versions).
File: node_modules/whatsapp-web.js/src/util/Injected/Utils.js
Changes:
- Added check for
markedUnreadproperty existence before using it - Detects special chat types (channels and status broadcasts)
- Implements multiple fallback layers:
- Try
sendSeen(chat)ifmarkedUnreadexists and not a channel/status - Fall back to
markSeen(chat)if available - Catch any errors and retry with
markSeen
- Try
- Ensures stream is always marked unavailable using
finallyblock
- Prevents crashes when sending messages
- Handles all chat types gracefully
- Provides robust error handling
- Related issue: https://github.com/pedroslopez/whatsapp-web.js/issues
- Similar fix proposed by community developers
Multiple webhook events are being fired for the same action, particularly:
- Duplicate
authenticatedevents - Duplicate
readyevents - Multiple event bindings causing repeated callbacks
This occurs because WhatsApp Web's hasSynced state can toggle multiple times (true → false → true), triggering the initialization flow repeatedly.
Using Fork: github:timothydillan/whatsapp-web.js#fix/duplicate-events-and-bindings
Instead of patching the official version, we use @timothydillan's fork which includes:
- Guard flags to prevent duplicate event listeners
- Proper handling of restored sessions (important!)
- Better CDP binding management after page navigation
- Updated
sendSeenformat with{chat, threadId}structure
Why Fork Instead of Patch?
- The duplicate events patch caused the READY event to never fire on restored sessions
- This blocked all message reception
- Timothy's implementation handles session restoration correctly
- Prevents duplicate webhook calls
- Ensures clean single initialization
- Works correctly with session restoration
- Reduces unnecessary API calls to webhook endpoints
- Fix by @timothydillan: https://github.com/timothydillan/whatsapp-web.js/tree/fix/duplicate-events-and-bindings
- Addresses duplicate event issues in WhatsApp Web 2.3000.x+
File: src/sessions.js (local project file, not in patch)
Changes: Filters non-critical WhatsApp CDN message history download failures from error logs to debug level.
Problem: ERR_ABORTED errors from cdn.whatsapp.net/mms/md-msg-hist clutter logs during sync.
Solution: These expected failures are now logged at debug level instead of error level.
Patches are automatically applied after every npm install via the postinstall script in package.json:
"scripts": {
"postinstall": "patch-package"
}The patch-package tool reads patch files from the patches/ directory and applies them to the installed packages in node_modules/.
Monitor the official repository for fixes:
- https://github.com/pedroslopez/whatsapp-web.js/releases
- https://github.com/pedroslopez/whatsapp-web.js/commits/main
Look for commits or releases that mention:
- "markedUnread fix"
- "duplicate events fix"
- "duplicate READY events"
- Similar issues
Once you confirm the official version includes the fixes:
# Install the official version
npm install whatsapp-web.js@1.35.0 # Replace with actual fixed versionUpdate package.json to remove the fork reference:
{
"dependencies": {
"whatsapp-web.js": "^1.35.0" // Instead of github:timothydillan/...
}
}# Test if the markedUnread issue is fixed
npm run start
# Send test messages
# If markedUnread is also fixed, delete the patch file
rm patches/whatsapp-web.js+1.34.3.patch
# If markedUnread still needs fixing, the patch will be reapplied automatically
# Just update the patch file name if the version changedAfter removing patches, test these scenarios:
-
Message Sending:
# Send a message via API curl -X POST http://localhost:3000/client/sendMessage/YOUR_SESSION \ -H "Content-Type: application/json" \ -d '{"chatId": "1234567890@c.us", "contentType": "string", "content": "Test message"}'
-
Check for Duplicate Events:
- Monitor your webhook endpoint
- Verify only ONE
readyevent is received - Verify only ONE
authenticatedevent per session initialization
-
Check Logs:
- Ensure no
markedUnreaderrors appear - Verify no excessive CDN error logs
- Ensure no
If patches are no longer needed and you want to remove patch-package:
# Remove patch-package from devDependencies
npm uninstall patch-package
# Remove postinstall script from package.json
# Edit package.json and remove: "postinstall": "patch-package"
# Remove patches directory
rm -rf patches/To verify the patches are correctly applied:
# Check if patch file exists
ls -la patches/
# Verify patch-package is installed
npm list patch-package
# Check postinstall script
grep postinstall package.json
# Test by reinstalling dependencies
rm -rf node_modules
npm install
# Should see: "patch-package 8.0.1" output during installIf you see errors during npm install:
**ERROR** Failed to apply patch for package whatsapp-web.js
Causes:
- whatsapp-web.js version changed (no longer 1.34.4)
- Official package already includes the fixes
- File structure changed in the package
Solutions:
- Check current installed version:
npm list whatsapp-web.js - If version is different, regenerate patch:
# Make manual changes to node_modules/whatsapp-web.js npx patch-package whatsapp-web.js - Or remove the patch and test if fixes are needed
If patch-package is not found:
npm install --save-dev patch-packageIf you find issues with these patches or want to improve them:
- Make changes to the appropriate files in
node_modules/whatsapp-web.js/ - Regenerate the patch:
npx patch-package whatsapp-web.js
- Test thoroughly
- Commit the updated patch file
- whatsapp-web.js Official Repository: https://github.com/pedroslopez/whatsapp-web.js
- patch-package Documentation: https://github.com/ds300/patch-package
- Duplicate Events Fix Source: https://github.com/timothydillan/whatsapp-web.js/tree/fix/duplicate-events-and-bindings
| Date | Patch Version | whatsapp-web.js Version | Description |
|---|---|---|---|
| 2026-01-28 | 1.0 | 1.34.4 | Initial combined patch (markedUnread + duplicate events) |
Last Updated: 2026-01-28
Maintainer: Project Team
Status: