Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
-5
rated 0 times [  1] [ 6]  / answers: 1 / hits: 48253  / 9 Years ago, sun, november 8, 2015, 12:00:00

This is a follow-up question to In Node.js, how do I "include" functions from my other files?



I would like to include an external js file that contains common functions for a node.js app.



From one of the answers in In Node.js, how do I "include" functions from my other files?, this can be done by



// tools.js
// ========
module.exports = {
foo: function () {
// whatever
},
bar: function () {
// whatever
}
};

var zemba = function () {
}


It is inconvenient to export each and every function. Is it possible to have a one-liner that exports all functions? Something that looks like this;



module.exports = 'all functions';


It is so much more convenient this way. It is also less buggy in case one forgets to export certain functions later.



If not a one-liner, are there simpler alternatives that make coding more convenient? I just want to include an external js file made up of common functions conveniently. Something like include <stdio.h> in C/C++.


More From » node.js

 Answers
14

You can write all your function declarations first and then export them in an object:


function bar() {
//bar
}

function foo() {
//foo
}

module.exports = {
foo, bar
};

There's no magical one-liner though, you need to explicitly export the functions you want to be public.


[#64475] Wednesday, November 4, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reedmustafam

Total Points: 211
Total Questions: 83
Total Answers: 105

Location: Vanuatu
Member since Wed, Oct 14, 2020
4 Years ago
;