4

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

Sush
  • 1,449
  • 8
  • 26
  • 51
  • I think the filename for the require is case sensitive. Check your case and spelling. Also, try removing the leading ./ in the require if everything is in the same directory. – user949300 Nov 01 '13 at 06:56
  • 4
    Your `objTest` should be `objTest2` in test1.js – user568109 Nov 01 '13 at 07:07
  • Possible duplicate of [In Node.js, how do I "include" functions from my other files?](https://stackoverflow.com/questions/5797852/in-node-js-how-do-i-include-functions-from-my-other-files) – Towerss Sep 05 '17 at 04:24

2 Answers2

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

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

    console.log(time);
    console.log(jobid);
divz
  • 7,847
  • 23
  • 55
  • 78
2

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();
Nija I Pillai
  • 1,046
  • 11
  • 13