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.)

2 Answers 2

5

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); 
Sign up to request clarification or add additional context in comments.

5 Comments

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 - 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 (Here's a good SO reference). 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".
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.
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 you would need to access your exported members by assigning the require call to a name, I've updated my answer with an example.
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 

1 Comment

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);} ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.