Sush
Sush

Reputation: 1457

call a function from one file in node.js

i have a file named test2.js created a function inside it

var schedule = require('node-schedule');   
var jobScedule = function(time, jobid) {
  schedule.scheduleJob(time, function(jobid){

console.log("scheduling starts");
});

}
exports.jobScedule = jobScedule;

in test1.js i have to call this function.

var objTest2 = require("./Test2.js");
var time = new Date();
var jobid=10;
objTest.jobScedule(time,jobid)

console.log(time);
console.log(jobid);

i dont know to call function from another file in node.js.rectify me

Upvotes: 4

Views: 8298

Answers (2)

Nija I Pillai
Nija I Pillai

Reputation: 1136

In 'user.js', contains functions to be exported like

module.exports = {
  func1: function () {
    // func1 
  },
  func2: function () {
    // func2 
  }
};

In the other admin.js files, you can include the 'user.js' by

const user = require('./user');

In the following code, the exported funct1 can be like

user.func1();

Upvotes: 2

divz
divz

Reputation: 7957

var objTest= require("./Test2.js");

    var time = new Date();
    var jobid=10;
    objTest.jobScedule(time,jobid)

    console.log(time);
    console.log(jobid);

Upvotes: 4

Related Questions