Creating Schedules in Node.js with the Cron library

12 September 2023


In your Node.js server, you may wish to run code at certain times, whether that be at the start of every month, at the start of every hour, at the twelth hour of every day or some other scheduled time.

The Node.js Cron Library

The Node.js Cron library provides a simple and powerful way to schedule recurring tasks in your Node.js applications. It allows you to define cron patterns to specify when and how often a task should run. Whether you need to send periodic emails, update database records, or perform any other repetitive task, the Node.js Cron library makes it easy to automate these processes at specific times.

To get started, you'll need to install the Node.js Cron library using npm, the Node package manager (or a package manager of your choice). Open your terminal and run the following command:

npm install cron

Once installed, you can import the library in your Node.js application using the require function for CommonJS applications:

const { CronJob } = require('cron');

Now, we can create a job to run at a set time. Suppose we want to log a message to the console every day at 8:00 AM. We can define a new CronJob instance with the desired cron pattern and the function to be executed:

const job = new CronJob('0 8 * * *', () => {
  console.log('Good morning! Have a nice day!');
}, null, true, 'America/New_York');

Let's run through the parameters we are using here. The first parameter is the cron pattern ('0 8 * * *'). You can use tools such as crontab to help you write cron patterns.

The second parameter is the function to run every time the cron job should execute. In our case we use an arrow function and call the console.log function to display a message in the terminal.

The third parameter is an optional function that will be run if you ever stop the job with job.stop(). In our case we don't need this so it is set to null.

The fourth parameter is a boolean value saying whether or not the job should automatically start. By setting it to true, the job will automatically start executing as soon as we create it, if it was not set to true we would have to call job.start() to start the job.

Finally, the fifth parameter is the timezone, in our case we use the tme in New York to base our schedule on. Some examples of other timezone are Europe/London, Europe/Paris, Australia/Sydney and Asia/Bangkok.

Important notes

Due to the libary being written in Node.js it may not run exactly as you expect compared to regular cron jobs.

Some of the key things to note are:

Why use the Node.js cron library?

You may be wondering why you would want to use the Node.js cron library rather than registering normal cron jobs and the main reason is to enable you to easily run scheduled tasks within the same process as your main program. This means you can easily interact with the memory of your running program.

Another reason is that it is easier for your code to automatically register schedules that will run as long as the program is running.

Why use cron jobs at all?

You may be wondering why you should use Cron jobs over a simple setInterval and the answer is that it allows you to run a function at a certain guaranteed time. With setInterval if your program restarts your timer will also restart, whilst with Cron if your program restarts it will continue waiting for the next execution of the job.


Explore more posts