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

3
  • 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. Commented Nov 1, 2013 at 6:56
  • 4
    Your objTest should be objTest2 in test1.js Commented Nov 1, 2013 at 7:07
  • Possible duplicate of In Node.js, how do I "include" functions from my other files? Commented Sep 5, 2017 at 4:24

2 Answers 2

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

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

    console.log(time);
    console.log(jobid);
Sign up to request clarification or add additional context in comments.

Comments

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.