I have been using module.exports.funcName = () => { } syntax fo my apps until I discovered exporting the object at the end with all the functions name as keys.
var func1 = ( ) => { }
var func2 = ( ) => { }
module.exports = {
"func1": func1,
"func2": func2
}
Which is more correct? I know we can add properties to object dynamically in Javascript, but is it wrong in any way? Will the latter method overwrite exports variable if it already contains anything?
Thanks
JS enthusiast
Peter Scheler
Depends on your version target.
Your example is exactly correct for Node < v4 (aside from the arrow functions). Since Node v4 you can make it a bit simpler if you don't need to rename your export properties:
const func1 = ( ) => { } const func2 = ( ) => { } const funcThree = ( ) => { } module.exports = { func1, func2, func3: funcThree }And with Node v10 LTS you should use:
export const func1 = ( ) => { } export const func2 = ( ) => { } export const funcThree = ( ) => { } // And optional/aditional: export default { func1, func2, func3: funcThree, }