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 trialErik Nuber
20,629 PointsUnderstanding 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
Branden Dane
7,000 PointsWith 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.
Erik Nuber
20,629 PointsGreat. Thank you for the well thought out answer.
Branden Dane
7,000 PointsNo problem.
Justin Coen
Full Stack JavaScript Techdegree Student 12,829 PointsJustin Coen
Full Stack JavaScript Techdegree Student 12,829 PointsThanks 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.