Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

305
Views
How to Run a Cron Job when the code is in Other File - Node JS

I am trying to execute a cron every 1 hour.

For which I have initiated the cron job in my index.js file as below

const cron = require('node-cron');
const readDocuments = require("./cronJobs/readDocuments");
var task = cron.schedule('0 0 */1 * * *', readDocuments);

task.start();

Where as the actual code to be executed has been written in ./cronJobs/readDocuments and the code inside this file is below, where I am trying to read a csv file.

readDocuments.js

const ResponseManager = require("../common/ResponseManager");

var fs = require('fs');
const csv = require('csv-parser');
console.log('Read Document....')
try {

var filepath = "../files/abc.csv";
fs.createReadStream(filepath)
.on('error', () => {
// handle error
})
.pipe(csv())
.on('data', (row) => {
console.log('rowrowrow',row)
})
.on('end', () => {
// handle end of CSV
})
} catch (error) {
console.log('errorerror',error)
res.status(500).json(ResponseManager(false, error.message));
}

When I run the node js in cmd with node index.js, the console Read Document.... is getting displayed but the other code is not getting executed. It is giving me throw 'execution must be a function'; error

How do I resolve this or what is the actual procedure to execute the code inside a file with cron job from index.js.

Please help. Thanks !!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you need to export the cron function and consume it on your file

about 4 years ago · Juan Pablo Isaza Report

0

The second argument to cron.schedule() must be function. You need to wrap the code into a function and export it from the module.

const ResponseManager = require("../common/ResponseManager");
const fs = require('fs');
const csv = require('csv-parser');

module.exports = function () {
  console.log('Read Document....')
  try {
    const filepath = "../files/abc.csv";
    fs.createReadStream(filepath)
      .on('error', () => {
        // handle error
      })
      .pipe(csv())
      .on('data', (row) => {
        console.log('rowrowrow', row)
      })
      .on('end', () => {
        // handle end of CSV
      });
  } catch (error) {
    console.log('errorerror', error)
    res.status(500).json(ResponseManager(false, error.message));
  }
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!