JavaScript Node.js Basics Handling Errors in Node Organizing Your Code with require

Mezzo Forte
Mezzo Forte
1,877 Points

how to access more then one function

Noticed that the getProfile was set to the get with:

 module.exports.get = getProfile;

and called on the app.js side with:

 const profile = require('./profile.js');
 const users = process.argv.slice(2);
 users.forEach(profile.get);

Say I have more then one function in the profile.js module. How would I access that as well if I could only call profile.get. and profile.get has already been assigned to getProfile.

Thank you

Mezzo Forte
Mezzo Forte
1,877 Points

Nevermind after proceeding to the Questions after the video it made sense? Though I would readly love clarification. The:

 module.exports.get = getProfile;
 module.exports.say= getProfile;
 module.exports.getProfile = getProfile;

the get can be changed. I've tried replacing it with say, getProfile, and other words and it worked fine after accessing it on the app.js side. The get is a property being created then accessed in app.js using the below:

 users.forEach(profile.get);
 users.forEach(profile.say);
 users.forEach(profile.getProfile);

1 Answer

Tim Gavlick
Tim Gavlick
11,724 Points

It also helps to think of module.exports as an object you're returning from the module. As such, if you have more than one export:

module.exports.get = getProfile;
module.exports.something = something;

You might consider rewriting like:

module.exports = {
  get: getProfile,
  something: something
}
Kenneth Kim
Kenneth Kim
3,793 Points

Tim, so you mean to say that the exports can be formatted to a JSON Object? Afterwards are there any steps to do? Let say for example I want to export 3 methods

module.exports = {
  get: getName,
  get: getAge,
  get: getAddress
}

//module.start export() or something ? or the above code is enough? Thanks. Also by doing export, does it mean that all exported functions are visible to all other JS files?