Can I export more than one object using module.exports?


#1

Question

Can I export more than one object using module.exports?

Answer

We can export more than one object using module.exports by giving the object we export properties that are assigned to objects, a few examples:

  1. Creating an object to export where the properties are assigned to other objects:
let MainObject = {};

MainObject.objToExport = {
  propOne: 1,
  propTwo: 2
};

MainObject.anotherObjToExport = {
  anotherProp: 3,
  andAnother: 4
};

module.exports = MainObject;
  1. Assigning module.exports multiple properties:
module.exports.objToExport = {
  propOne: 1,
  propTwo: 2
};

module.exports.anotherObjToExport = {
  anotherProp: 3,
  andAnother: 4
};
  1. Assigning module.exports to an object with multiple properties:
module.exports = {
  objToExport: {
    propOne: 1,
    propTwo: 2
  },
  anotherObjToExport: {
    anotherProp: 3,
    andAnother: 4
  }
};