Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Node.js Basics 2017 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,725 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,795 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?