title: How to Run a Cron Job when the code is in Other File - Node JS
body: <p>I am trying to execute a cron every 1 hour.</p>
  <p>For which I have initiated the cron job in my <code>index.js</code> file as below</p>
  <pre><code>const cron = require('node-cron');
  const readDocuments = require(&quot;./cronJobs/readDocuments&quot;);
  var task = cron.schedule('0 0 */1 * * *', readDocuments);
  
  task.start();
  </code></pre>
  <p>Where as the actual code to be executed has been written in <code>./cronJobs/readDocuments</code> and the code inside this file is below, where I am trying to read a csv file.</p>
  <p><em><strong>readDocuments.js</strong></em></p>
  <pre><code>const ResponseManager = require(&quot;../common/ResponseManager&quot;);
  
  var fs = require('fs');
  const csv = require('csv-parser');
  console.log('Read Document....')
  try {
  
  var filepath = &quot;../files/abc.csv&quot;;
  fs.createReadStream(filepath)
  .on('error', () =&gt; {
  // handle error
  })
  .pipe(csv())
  .on('data', (row) =&gt; {
  console.log('rowrowrow',row)
  })
  .on('end', () =&gt; {
  // handle end of CSV
  })
  } catch (error) {
  console.log('errorerror',error)
  res.status(500).json(ResponseManager(false, error.message));
  }
  </code></pre>
  <p>When I run the node js in cmd with <code>node index.js</code>, the console <code>Read Document....</code> is getting displayed but the other code is not getting executed.
  It is giving me <code> throw 'execution must be a function';</code> error</p>
  <p>How do I resolve this or what is the actual procedure to execute the code inside a file with cron job from index.js.</p>
  <p>Please help. Thanks !!</p>