Creating and Using Modules (CommonJS and ES6 Modules)
This tutorial explains how to utilize both CommonJS and ES6 models in Node.js. It demonstrates importing the built-in 'os' model using both methods.
Lets Go!

Creating and Using Modules (CommonJS and ES6 Modules)
Lesson 10
Learn the basics of creating and using modules in Node.js with CommonJS and ES6 module syntax.
Get Started 🍁Introduction to Node.js Models: CommonJS vs. ES6
Welcome to "Introduction to Node.js Models"! In this course, we will explore how to work with common JS and ES6 models in Node.js.
Node.js has historically used CommonJS models by default, but did you know that you can also leverage ES6 models in your Node.js projects?
Have you ever wondered how you can import the os
(operating system) model, a commonly used model in Node.js, into an ES6 model?
Throughout this course, we will walk you through the traditional way of working with CommonJS models using the required
function, as well as demonstrate how to use ES6 models by using the import
syntax.
By the end of this course, you will have a clear understanding of how to seamlessly integrate CommonJS and ES6 models in Node.js, opening up a world of possibilities for your projects.
Are you ready to dive into the world of Node.js models and explore the differences between CommonJS and ES6? Let's get started!
Main Concepts of Node.js Models
-
CommonJS Models
- By default, Node.js historically uses CommonJS types of modules.
- CommonJS modules can be imported using the
require
function. - Demonstrated using the built-in
os
(operating system) model as an example. - Example: importing the
os
model usingrequire
and then calling methods on the object.
-
ES6 Models
- ES6 modules can also be used in Node.js alongside CommonJS modules.
- ES6 modules are imported using the
import
syntax. - While the
os
model is a CommonJS model, it can still be imported into an ES6 module. - It is not possible to import ES6 models into CommonJS modules.
- Extension of the file needs to be changed to
.mjs
for Node.js to recognize it as an ES6 model. - Alternatively, the
type
property inpackage.json
can be changed tomodule
to indicate an ES6 model. - Example: importing the
os
model in an ES6 module usingimport os from 'os'
.
-
Compatibility
- Node.js needs to be updated to the latest version to ensure compatibility with ES6 modules.
- It is recommended to use Node.js version 15 or above for smooth execution.
- Ensuring the right version of Node.js avoids encountering errors in using both types of models in Node.js.
Practical Applications of Model Usage in Node.js
In this section, we will walk through how to effectively use both CommonJS and ES6 models in Node.js for the os
operating system model.
Using CommonJS Models:
- Start by utilizing the historical way of importing CommonJS models:
const os = require('os');
- Next, call the desired method on the imported CommonJS model:
console.log(os.version());
- Execute the code to display the OS version.
Using ES6 Models:
-
Comment out the CommonJS model import to switch to ES6 model import:
// const os = require('os'); import os from 'os';
-
Change the file extension to
.mjs
to specify that this is an ES6 model:node filename.mjs
Tip: You can also update the
type
property inpackage.json
to"module"
to inform Node.js that this is an ES6 model. -
Run the code to print the OS version.
-
Remember to use the latest version of Node.js (e.g., version 15) to avoid encountering errors.
Now, give it a try yourself! Follow the steps outlined above to seamlessly utilize both CommonJS and ES6 models in your Node.js projects. Your understanding of model usage will significantly benefit from this hands-on practice. Happy coding!
Test your Knowledge
Which keyword is used to export a function in CommonJS modules?
Which keyword is used to export a function in CommonJS modules?
Advanced Insights into Node.js Models
In this section, we will delve deeper into using both CommonJS and ES6 models in Node.js. Understanding how to work with both types of models can enhance the flexibility and efficiency of your Node.js applications.
CommonJS Models
Historically, Node.js has primarily used CommonJS models for module organization. To import a CommonJS model, you can utilize the require()
function. For example, if you want to import the os
(operating system) model in a CommonJS format, you can do so as follows:
const os = require('os'); console.log(os.version);
This approach is the traditional way of importing and using CommonJS models in Node.js applications.
ES6 Models
ES6 models offer a more modern and concise way of organizing and importing modules in Node.js. To import an ES6 module like the os
model, you can use the import
syntax:
import os from 'os'; console.log(os.version);
It's important to note that while you can import CommonJS models into ES6 modules, the reverse is not possible. Changing the file extension to .mjs
or specifying the module type in package.json
as ES6 can help Node.js recognize and run ES6 modules correctly.
Expert Tip: Node.js Version Compatibility
Ensure that you are using a compatible version of Node.js when working with ES6 modules. Using the latest version (such as Node.js version 15) can help avoid compatibility issues and errors when incorporating ES6 features.
Curiosity question: Can you explain the benefits of using ES6 modules over CommonJS in Node.js applications? How do these differences affect the structure and performance of your code?
With these insights into CommonJS and ES6 models in Node.js, you can optimize your module management and leverage the strengths of both module formats effectively. Experiment with importing different modules and explore the efficiencies each model offers in your Node.js projects.
Additional Resources for Node.js Models
If you're interested in learning more about using CommonJS and ES6 models in Node.js, here are some resources to help enhance your understanding:
-
Node.js Documentation: The official documentation for Node.js provides detailed information on modules, including CommonJS and ES6 models.
-
CommonJS Modules vs ES6 Modules: A helpful article that explains the differences between CommonJS and ES6 modules and how to use them effectively in Node.js.
-
ES6 Modules in Node.js: An in-depth blog post that covers how to use ES6 modules in Node.js, including configuration options and best practices.
By exploring these additional resources, you can deepen your knowledge of Node.js models and improve your skills in module management.
Practice
Task: Create a math.js module that exports the following functions:
- add(a, b)
- subtract(a, b).
Task: Create a main script that:
- Imports math.js using CommonJS syntax (require).
- Logs the results of calling add and subtract.
Task: Convert the module and script to use ES6 module syntax (import/export).