2

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,082
  • 2
  • 23
  • 48
Trees4theForest
  • 942
  • 2
  • 14
  • 41

2 Answers2

3

Use module.exportsto 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
}
thomaux
  • 18,290
  • 9
  • 74
  • 97
  • 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
2

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,082
  • 2
  • 23
  • 48