When you start working with WebPack for GameTime, you'll notice that you can't just define a variable in one file and find it in another as easily as you can in Rails.
Read Node.js, Require and Exports and Organize Your Code with RequireJS
- In the context of Node, what is a
module
? Amodule
is a the essential building block of Node. In node, amodule
maps directl to a file. The contents of said file are still private and node requires that any access to the files' contents be explicetely returned.
require
is used to load a module.
- The code examples from the second blog post look very different from the first. Why?
The code is vastly different because the second post ( treehouse blog ) introduces the
requireJS
library. This allows the developer to use adefine()
function. In short, anything within thedefine()
function is returned and can be exported to another file.
requireJS also allows you to specify what module dependencies will be needed. YOU can pass file name in the define()
fundtion as function parameters.
lastly, requireJS
gives us the require()
function which is run immediately(it is NOT stored as a module). The benefit of this is so we can assign all module depencies in main.js and require said file fro our HTML file easily.
Excellent!