This course offers an introduction to the CommonJS standard for Node.js modules
Learning Objectives
- Learn about the CommonJS standard
- Learn about the module object
- Learn how to implement a custom module using CommonJS
- Learn how to export and import a custom module
- Learn how to access and use a custom module method
Intended Audience
This course is intended for anyone who wants to learn about Node.js.
Prerequisites
There are no prerequisites for this course but an interest in Node.js would be beneficial.
Introduction to Node.js - CommonJS Modules. At the time of the making of this video, Node.js supports two module systems. The topic of this video, CommonJS, is the default standard, it is also the older standard. There has been no indication that this default standard will change in the near future or change at all. For maximum compatibility is highly recommended that you use the CommonJS format when writing modules. On the screen I have the lab environment open, I will begin by typing console log module. In this index.js file, using the Node command, I will run this file in the terminal by typing node index.js. Now in the terminal, I have the module object that provides general utility methods when interacting with instances of module. One thing to note is this Children array, which I will circle back to a little later in this video, and I'm going to create a module using CommonJS. I will make a new file in the lib folder, and I will name it math.js. And in this file I'm going to create two functions: first an add function, Const add equals parentheses, and there will be two Parameters, number one and number two arrow function, and this will return number one plus number two. Now, I will copy this function and paste it on the next line below. I will rename it to subtract and I will replace the plus sign with a minus sign. Now in order to use this module in other parts of my program, I need to export this module. Module exports equals an object, and this object will have two properties. First an add property with the value of add. This references the add function above, and I will repeat this again for the subtract function. Going back to the index page, now in order to access this module, I need to import it into this file. I will type const math = require, and as an argument I will pass the relative path to the math file. Note that I do not have to include the file names extension as part of this relative path. Now, before going further I will execute this file again to show the updated module in the console. Here is the new console log, and to the right I have a reference of the original console log that was done earlier in the video. Now in the Children array, there's another module. This module has a path to the lib folder, and the exports property is an object. So, what is module exports?
It is the object that's actually returned as a result of the required call that is currently safe to the math variable. Now, I can use this math variable to access the add and subtract function from the module exports object. I will replace the module in the console.log with math.add, passing in the arguments of five and three. I will copy and paste this console log, I will replace add with subtract, and now I'm going to execute this file in the terminal again. In the terminal, I have two values, eight and two. Now this works, but I'm going to refactor this module to take advantage of some ES6 syntax.
I'm going to go back to the math JS file. Inside of the module exports object, I'm going to remove add from the add property, and subtract from the subtract property. With the introduction of ES6, if a previously declared variable is used as an objects property name, you do not have to provide a property value. It is implied that the value of the property is the value given to the variable of the same name. Meaning that the add property's value is going to be the arrow function it was assigned to, and the same for the subtract property value. Returning back to the index file, I will execute this again in the terminal to demonstrate that this still works, which it does. Another ES6 feature is destructuring, and I'm going to change the math variable to an empty object, and I will destructure, add and subtract from the required statement. Now, I no longer need to use math in the console logs, so I will remove it, and I will execute this again, and it still works. And that's it, thanks for watching at Cloud Academy.
Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.
Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.
Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.
Farish is an avid powerlifter, sushi lover, and occasional Funko collector.