Introduction to Node.js - Using Third-Party Modules With CommonJS
Introduction to Node.js - Using Third-Party Modules With CommonJS

This course offers an introduction to working with third-party modules using the CommonJS standard. This course guides you with installing a third-party package, importing and exporting the module, and creating custom code that is integrated with the module.

Learning Objectives

  • Learn how to install a third-party module using NPM
  • Learn how to use the required syntax with a third-party module
  • Learn how to import the third-party module into a file to create custom functions
  • Learn how to export the module in order to use it in different parts of your application

Intended Audience

This course is intended for anyone who wants to learn about Node.js.


There are no prerequisites for this course but an interest in Node.js would be beneficial.


Introduction to Node.js - Using Third-Party Modules with  CommonJS. In this video, I'm going to cover how to use a third-party module as part of the CommonJS system. At different stages of the video, on the right side of the screen, there will be a reference to related documentation. I will begin by installing the cli color module. This module is designed to help with colors, formatting, and other features in the console. In the terminal, I will type 'npm install cli-color'.


Now the installation is complete, and the modules listed as a dependency in the package.json file. In the index file, in order to use this module, I need to import it using the required statement. 'const clc = require(cli-color);'. The documentation for this module has many use case examples. In the index file, I will type console.log( and I will pass in the text of 'This is my green text'.

Now in order to execute this file in the terminal, I will type 'node index.js.' Now the terminal displays a green-formatted text that states, "This is my green text". Now this works, but the drawback is that the entry point file, which is the index file, isn't meant to be used as a module. If I were to import this third-party module into multiple files, my code would be overly repetitive. The solution is to integrate this third-party module into a custom module.

I have a file called Color.js, and at the top, I will type in the required statement for the cli-color module. To make this a little easier to use, I'm going to make a function. 'const setColorText = () with two parameters; color and text. Then using an arrow function, I will console.log() the same module method that I used earlier in the index file, but I will make this a little bit more dynamic; 'clc[color]() and then in the parentheses, I will pass through a template string with a string expression of '${text}'. So, why did I write the place holder method '[Color]' this way?


Earlier in the video, when I wrote this method, I used the dot notation for the color, but here I'm using the square bracket notation. That's because in JavaScript, when you want to call methods based on variable string values, when working with objects, you have to use square brackets.

If you're wondering what object am I referring to, modules are objects, and module methods are object properties. Does this mean that by using square bracket notation I could call module methods dynamically?

Yes. Now, in order to make this function available to use, I need to export it. module.exports = { } and I will export setColorText. Now, I'm ready to test out my color module. Going back to the index file, I'm going to delete the code that was previously written. Now I'm going to import the color module. 'const' using destructuring { setColorText = require()}.

Now I need the relative path of where the modules is located at. './color'. When importing a third party module, you do not need to include the file extension. Now I can use the 'setColorText()' function; setColorText() and I will pass in two arguments; the string 'blue' for the color parameter, and the string, "This is my blue text" for the text parameter. Going to the terminal, I will execute node on the index file again, and they in blue text, 'This is my blue text'. And that's it. Thanks for watching. @Cloud Academy.

About the Author
Learning Paths

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.