module.exports that include all functions in a single line
Asked Answered
B

7

36

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

Bradybradycardia answered 8/11, 2015 at 0:56 Comment(1)
No. This is not how Javascript works. The way you mention is the most efficient way.Apheliotropic
A
48

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.

Authentic answered 8/11, 2015 at 1:6 Comment(1)
Thanks. I guess that is the best javascript can offer at the moment. It is one thing that makes me appreciate python.Bradybradycardia
L
27

I have done something like the following:

var Exported = {
   someFunction: function() { },
   anotherFunction: function() { },
}

module.exports = Exported;

I require it in another file and I can access those functions

var Export = require('path/to/Exported');
Export.someFunction();

This is essentially just an object with functions in it, and then you export the object.

Libertinage answered 8/11, 2015 at 1:46 Comment(3)
This solution is the least redundant.Nigro
However, this solution fails if you want one of your functions to call another one of your functions. You might get around the problem by using a class.Nigro
@Nigro How does it fail? To call anotherFunction in someFunction just do this.anotherFunction()Savagery
O
22

A really old question but I just had to solve the same issue myself. the solution I used was to define a Class inside the module to contain all my functions and simply export an instance of the class.

classes.js looks like this:

class TestClass
{
   Function1() {
        return "Function1";
    } 
    Function2() {
        return "Function2";
    }
}

module.exports = new TestClass();

app.js looks like this:

const TestClass = require("./classes");
console.log( TestClass.Function1);

just keep adding more functions to the class and they will be exported :)

Of answered 6/10, 2019 at 0:1 Comment(0)
M
16

It is worth noting that in ES6, you can now export functions like this:

export function foo(){}
export function bar(){}
function zemba(){}

Simply write export before the functions you want to export. More information here.

Mongolian answered 19/3, 2018 at 20:59 Comment(0)
G
14

If you use ES6 you can do something like that:

function bar() {
   //bar
}

function foo() {
   //foo
}

export default { bar, foo };
Gravitative answered 26/5, 2019 at 12:45 Comment(0)
S
5
const fs = require("fs")


var ExportAll = {

    deleteFile : function deleteFile(image,folder="uploads"){
    
        let imagePath = `public/${folder}/${image}`
    
        if (fs.existsSync(imagePath)){
            fs.unlinkSync(imagePath)
        }
    },


    checkFile : function checkFile(image,folder="uploads"){
    
        let imagePath = `public/${folder}/${image}`
    
        if (fs.existsSync(imagePath)){
            return true
        }
        else{
            return false
        }
    },
    

    rand : function(min=1,max=10) 
           {
        return Math.floor((Math.random() * max) + min)
          }
}

module.exports = ExportAll
 
Squally answered 27/12, 2020 at 8:12 Comment(1)
Please add an explanation to what you have tried and what isn't working to add some clarity to your problemGosselin
G
4

Import everything from a type module file that has functions that are exported.

Found here: https://javascript.info/import-export

myfile.js

export function myFunction()
{
// ................
}

export function myFunction2()
{
// ................
}

myfile2.js - import everything that is exported in the file

import * as myFunctions from './myfile.js';


// Usage 

    myFunctions.myFunction(); 
    myFunctions.myFunction2();
Guddle answered 14/5, 2021 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.