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

polentz
seal-mask
.a{fill-rule:evenodd;}techdegree
polentz
Full Stack JavaScript Techdegree Student 11,755 Points

module.exports and require clarification

So when the instructor does module.exports.get = get is the same as module.exports = get and then in the file that we require it we do const get = require("./profile")?

1 Answer

Michael Hulet
Michael Hulet
47,912 Points

Setting get to be exported like this:

profile.js
module.exports.get = get;

will make it so that whenever another file requires this one, they'll have to call .get on the object they're importing to access the get function, like this:

const profile = require("./profile.js");

profile.get();

However, setting module.exports to be get directly like this:

profile.js
module.exports = get;

will cause the object that require returns to just be the get function itself, so whenever another file requires this one, they'd use the get function like this:

const get = require("./profile.js");

get();