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 (2014) Building a Command Line Application Making a GET Request with http

Erik Nuber
Erik Nuber
20,629 Points

Understanding Node.js, Require and Exports

Just passing on a link that explains better the Require function being used in this video. I think it was kind of glossed over and, it is actually an important concept. Maybe gets explained a bit better later on but, found this information that explained it in a very easy to understand manner.

http://openmymind.net/2012/2/3/Node-Require-and-Exports/

That said, I still do not understand what we are actually loading as a module with this code?

var https = require("https");

2 Answers

With JavaScript we have access to Native Objects, which are methods built into JavaScript, and Host Object, which are methods that belong to a specific environment (i.e. Browser, NodeJS).

When writing JS in NodeJS the following references a Host Object: var https = require("https");

https being a module built into NodeJS.

When writing JS in the Browser or any other number of stacks, we can write our own modules. You can write your ownJS module and export it. A simple example would be:

function hello(){ alert(''HELLO WORLD"); }

module.exports.hello = hello;

Assuming the above code snippet was written on a document called hello.js located in the root folder of your project, you would reference it (require it) on the page you want like so: var hello = require("./hello");

Notice the significance in the syntax between the two. Native NodeJS modules do not require a file path. Modules you create, however, require you to include the correct file path to their location.

Hope this helps clear thing up.

Justin Coen
seal-mask
.a{fill-rule:evenodd;}techdegree
Justin Coen
Full Stack JavaScript Techdegree Student 12,829 Points

Thanks for the breakdown, Branden.

I had to come back to this video because I needed additional insight on the require() usage and your answer made it much clearer.

Erik Nuber
Erik Nuber
20,629 Points

Great. Thank you for the well thought out answer.

No problem.