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

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
  }
};
6 Likes

In response to solution 2: In the modules lesson I have tried to run the next code in the 1-airplane.js file:

module.exports.airPlane = {
myAirplane: ‘starFighter’
};
module.exports.airPlane2 = {
myAirplane: ‘Boeing’
};

but I get a reference error on starFighter. What am I doing wrong?

4 Likes

Hey @mjwmeijer after the default steps in the exercise, I copied and ran the same code you have written above and the only thing which gave me an error, first of all, was the values that got copied as ‘starFighter’ & ‘Boeing’ and not as “starFighter” & “Boeing”. So when I ran for the first time I got a “starFighter not defined” error but after removing the (‘ ’) with this (" ") from the values and running gave no error.

2 Likes

how about this way:
imagine a piece of code:

let test = 'hello';
class building {
 constructor(owner, floor, buildYear){
 this._owner=owner;
 this._floor=floor;
 this._age=(2019-buildYear)   }
 get owner(){return this._owner;}
 get floor(){return this._floor;}
 get age(){return this._age;}
};
let table = {
 legs:4,
 color:'brown',
 material:'wood'
};

You just choose whatever you want and export them as a massive:

module.exports = [test, building];

at the import side. You import your massive and use any part of it calling its element number:

const imp = require('./1-airplane.js');
let hilton = new imp[1]('pitter',12,1280);
console.log(hilton);
console.log(imp[0]);

it works for me but as I am a newbie, I’m not sure how correct is this method :yum:

5 Likes

I think you are on the right line but a better way of exporting your module this is the following:

module.exports = {
  building,
  table
}

When you require this you can use ‘.’ notation, e.g.:

const imp = require('./1-airplane.js');
let hilton = new imp.building('pitter',12,1280);
console.log(hilton);
console.log(imp.table);

This works are you are exporting a JavaScript Object with properties building and table and you do not need to know the Array element position to use a function or get some data as you can simply use the name. You can also import using require just the exported object property you want like this:

const buildingFunctionRenamed = require('./1-airplane.js').building
let table = require('./1-airplane.js').table
5 Likes

so I tried to export a little code locally… I read it’s not possible… any comments on that?

in order to get some comment which can help you, it is better to share your current url where you got stuck, and if it is possible, it is better to describe your situation specifically.
how far did you try, and why did you try that way, and what you expected as a result and how was the results and so on…

We can directly export multiple objects. This works too

let Airplane = {
  myAirplane: "StarJet"
};


let Airplane1 = {
  name: 'AirBus'
};

module.exports = {
  Airplane,
  Airplane1
};
2 Likes