3

My super-long file (main.js) works fine as is. But I want to split out the functions dealing with 'y' into a separate file for organization. In PHP I would use require('yfunctions.php') and be done with it.

Is there an equivalent in javascript that doesn't require rewriting the function calls?

main.js:

// do stuff

function first(x){
  // do stuff with x
}

function second(y){
  // do stuff to y
  // return y
}

function third(y){
  // do stuff with y
}

ultimately becomes:

main.js:

require('yfunctions.js');
// do stuff

function first(x){
  // do stuff with x
}

yfunctions.js:

function second(y){
  // do stuff to y
  // return y
}

function third(y){
  // do stuff with y
}

The above does not work (it seems). Do I have to add an "exports" declaration to each function in yfunctions.js? Is there not a way to say "export every function in this file as a function?"

(Note, I'm working with node.js / electron ... but I'm curious for general knowledge about how javascript works.)

Sayuri Mizuguchi
  • 5,250
  • 3
  • 26
  • 53
Trees4theForest
  • 1,267
  • 2
  • 18
  • 48

2 Answers2

4

Use module.exports to export members of a module. In your example:

module.exports.second = second;
module.exports.third = third;

function second(y){
  // do stuff to y
  // return y
}
    
function third(y){
  // do stuff with y
}

There's no option to automatically export all members of a module.

If you're working in ES6, the above could be simplified to:

module.exports = {
  second,
  third
};

function second(y){
  // do stuff to y
  // return y
}
    
function third(y){
  // do stuff with y
}

Lastly, in your main.js you can call the exported functions of other modules by assigning a name to the require statement:

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

yfunctions.second(y);
thomaux
  • 19,133
  • 10
  • 76
  • 103
  • 1
    Ugh... so if I'm splitting out 50 functions, I have to have a list of 50 module.exports? Seems redundant and tedious (and encourages me to make just one monster-long main.js file...) – Trees4theForest Apr 12 '17 at 12:32
  • 3
    @Trees4theForest - That is correct. `module.exports` and the newer (though largely unsupported at this time) ES6 `export` are heavily based on what is known as the [_Revealing Module Pattern_](https://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript) ([Here's a good SO reference](http://stackoverflow.com/questions/5647258/how-to-use-revealing-module-pattern-in-javascript)). In most cases you would try to export as few functions as you can and keep any implementation-specific functions hidden in the module itself, thus creating a sort of "public API". – Joshua Kleveter Apr 12 '17 at 12:44
  • 3
    There are several valid reasons not to just leave it as one long file. If you are using a reasonable text editor, you may have refactoring tools that can help with this process, and if not, a simple enough regex can grab them for you. I would encourage you to modularize your node software if the only stumbling block is collecting 50 function names. – Rafael Kennedy Apr 12 '17 at 12:46
  • Just to be clear after the `module.exports.second = second;` within `main.js` there is no more of a need to write *modulename.second(y)* or something equivalent right? That all the function calls to second() sent to main.js get passed to the module function? – Vass Jun 14 '22 at 07:29
  • 1
    @Vass you would need to access your exported members by assigning the require call to a name, I've updated my answer with an example. – thomaux Jun 14 '22 at 07:43
3

In this case you have to use module exports, and use require to exports the functions in other archive. And after you can use, check my example

functions.js

module.exports = {
  foo: function () {
    // do something
  },
  bar: function () {
    // do something
  }
};

var tryit = function () {
}

Use functions from functions.js

var callFunction = require('./functions');
console.log(typeof callFunction .foo); // => 'function'
console.log(typeof callFunction .bar); // => 'function'
console.log(typeof callFunction .tryit); // => undefined because does not use exports
Sayuri Mizuguchi
  • 5,250
  • 3
  • 26
  • 53
  • in order to use the functions from `functions.js` which are sent to `main.js` is it necessary to do anything else in `main.js` than require the module? Does `main.js` have to import each function header or delegate the call eg. function a(x){ foo(x);} ? – Vass Jun 14 '22 at 07:33