1

I have several javascript files that contain different parts of my node application. They are required using the logic below. I want to access a function in one file1.js from file2.js, but have had little success so far. Any help would be appreciated, thanks.

app.js: This is the file where I start my server and include all the express routes.

require(path_to_file+'/'+file_name)(app, mongoose_database, config_file);  //Require a bunch fo files here in a loop.

file1.js: This is an example file that is required using the code above.

module.exports = function(app, db, conf){
  function test() {  //Some function in a file being exported.
    console.log("YAY");
  }
}

file2.js: This is another file required using the code above. I want to access a function in file1.js from within this file (file2.js).

module.exports = function(app, db, conf){
  function performTest() {  //Some function in a file being exported.
    test();
  }
}
Scott
  • 993
  • 3
  • 10
  • 28

3 Answers3

4

file1.js

module.exports = function(app, db, conf){
  return function test() {
    console.log("YAY");
  }
}

Note you need to return the function.

file2.js

module.exports = function(app, db, conf){
  return function performTest() {
    var test = require('./file1')(app, db, conf);
    test();
  }
}

(some other file)

var test = require('./file2')(app, db, conf);
test();

or

require('./file2')(app, db, conf)();
David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • Thanks for the reply. This method works if I have one function in a module.export, however if I have multiple functions in a file all in a module.export, how do I access those. Is there a way to access the functions like this: `var file1 = require('./file1')(app,db,conf);` and then `file1.test()` – Scott Nov 13 '12 at 22:14
  • 1
    Sure. Just have your exported function return an object with the property `test` whose value is a function. The discussion from [this](http://stackoverflow.com/questions/13151693/passing-arguments-to-require-when-loading-module) question may be helpful. – David Weldon Nov 13 '12 at 22:30
1

The function you have right now in file1 is scoped only to the function within the exports. Instead, you want to export an object with each function being a member of that object.

//file1.js
module.exports = {
    test: function () {
    }
};


//file2.js:
var file1 = require('./file1.js');
module.exports = {
    performTest: function () {
        file1.test();
    }
}
Max
  • 8,671
  • 4
  • 33
  • 46
  • 1
    Hey I tried playing around with this method, but I couldn't get it to work. If I use your exact syntax I get an error point at the unexpected parenthesis `()` after the word `funtion` – Scott Nov 13 '12 at 22:16
0

In ECMAScript6 and above editions you can do it by export and import keywords.

First,implement and export an arrow function in a separate js file:

//file1.js
export const myFunc = () => {
    console.log("you are in myFunc now");
}

Then,import and call it in other files:

//otherfile.js

import { myFunc } from 'file1';

myFunc();

If you want to more about Arrow functions

fbarikzehy
  • 4,885
  • 2
  • 33
  • 39