Published on

Node.js and Defer.run: Scheduling CRON Jobs Made Easy

Authors

defer.run is platform to run background jobs. We don't have to worry about infrastructure. It supports pretty much all the tech stack in Node.js (from the latest Serverless Next.js to good old Express monolith app)

You can think of this as a Heroku for background jobs. And it plays well with a serverless environment.

It has first class support for running CRON jobs. Here is how to do it:

npm install @defer/client

Now we need create background function that needs to be run and configure the CRON timing in the default export of the function defer.cron

// defer/scheduledJob.js
import { defer } from "@defer/client"

const scheduledJob = async () => {
  // Your code here
  console.log("It works!")
};

// 12AM, UTC - https://crontab.cronhub.io/
export default defer.cron(scheduledJob, '0 0 * * *');

If this is your first time, you can refer their docs on how to setup your project

Reference

How to set up a Node.js CRON

Happy running CRON!