Cron Jobs in Node.js: The Practical Guide Nobody Gave Me
I spent 3 hours debugging why my cron job only ran once. Here's everything I wish I'd known before starting.
Why This Matters
Every serious application needs scheduled tasks:
Send daily digest emails at 9 AM
Clean up temp files every Sunday
Generate reports on the 1st of each month
Check API health every 5 minutes
I've tried every approach. Here's what works, what doesn't, and what I use in production.
Option 1: node-cron (Best for Most Cases)
npm install node-cron
Enter fullscreen mode
Exit fullscreen mode
The gold standard for Node.js scheduling:
const cron = require ( ' node-cron ' );
// Run every day at 9:00 AM cron . schedule ( ' 0 9 * * * ' , () => { console . log ( ' Sending daily digest... ' ); sendDailyDigest (); });
// Run every 5 minutes cron . schedule ( ' */5 * * * * ' , async () => { const status = await checkAPIHealth (); if ( ! status . ok ) alertAdmin ( status ); });
// Run on Mondays at 8:00 AM cron .…