Search code examples
javascriptcommonjs

How export all variable on commonjs from deferent file in one place


i have folder with contain file and each file have export module like below

myFirst.js

 const myName=()=>({name:"helo"})
 const myAddress=()=>({address:"Bali"})

module.export={
  myName,
  myAddress
}

mySecond.js

const myBisnis=()=>({name:"mouse bisnis"})

module.export ={
  myBisnis
}

all the module from myFirst.js and mySecond.js

myLast.js

module.export={
  myFirst,
  mySecond

}

and when i call myLast.js it will contail all the module from myFirst.js and secode.js .

thanks for your replay


Solution

  • If you want to export a function or something that does not exist in the file, you should import it first. Like this:

    //myLast.js
    
    import firstModule from 'myFirst.js';
    import secondModule from 'mySecond.js';
    
    module.export = {
      firstModule,
      secondModule
    };
    

    Or you may use a direct export:

    // myLast.js
    
    export { myName, myAddress } from 'myFirst.js';
    export { myBisnis } from 'mySecond.js';