Implementing Modules in Node
Today was Lego day 😍. I bought this expensive Art Project from Copenhagen Aiport on my way back to Dublin (Yes, it is cheaper on Amazon 😏) Today, I decided to open it and it was awesome.. ❤️ It was #buildTogether with my husband ;)
Now let’s talk Modules :D
What are Modules?
Modules are reusable pieces of code in a file that can be exported and then imported for use in another file. A modular program is one whose components can be separated, used individually, and recombined to create a complex system.
The above diagram illustrates breaking down of the entire program within
myApp.js into separate components that each handle a particular task like database, server operations, and date value conversions.
The modular strategy is termed as separation of concerns and is useful for many reasons:
- find, fix, and debug code more easily.
- reuse and recycle defined logic in different parts of the application.
- keep information private and protected from other modules.
- prevent pollution of the global namespace and potential naming collisions, by cautiously selecting variables and behavior loaded into a program.
Implementing modules in a program requires management via the following :
- To use the Node.js
module.exportsobject to export code from a file - meaning its functions and/or data can be used by other files/modules.
- To use the Node.js
require()function to import functions and/or data from another module.
- The Node runtime environment and the
- The browser’s runtime environment and the ES6
This article focuses on using the
require() syntax in the Node runtime environment.
Modules in Node
Consider following two programs that use the same function
celsiusToFahrenheit and is writing twice.
Executing the above code would be like:
$ node temperatures.js
The freezing point of water in Fahrenheit is 32
The boiling point of water in Fahrenheit is 212
Executing this code will give the below response:
$ node celsius-to-fahrenheit.js 100
100 degrees Celsius = 212 degrees Fahrenheit
Creating a module that exports a
celsiusToFahrenheit() function that can be used by both of these programs would solve this repetitive code problem.
To create a module, the same function is declared in a new file. Then, to make this function available to other files, its added as properties to the built-in
Two approaches are depicted, one is to assign an already-defined function to
module.exports.celsiusToFahrenheit. In the second, a new function expression is declared and assigned to
module.exports.fahrenheitToCelsius doing the opposite conversion.
module.exports object is built into the Node.js runtime environment. Other files can import this object, with another feature built into the Node.js runtime environment: the
require() function, and make use of these two functions.
require() function accepts a string as an argument. That string provides the file path to the module you would like to import.
Let’s update temperatures.js such that it uses
require() to import the
.celsiusToFahrenheit() method from the
module.exports object within converters.js:
In this case,
./ is a relative path indicating that converters.js is stored in the same folder as temperatures.js. Using
require(), the entire
module.exports object is returned and stored in the variable
converters. This means that both the
.fahrenheitToCelsius() methods can be used in this program.
Using Object Destructuring for selective exports with require()
In many cases, modules export a large number of functions but only one or two of them are needed. Object destructuring can be used to extract only the needed functions.
The program works the same way but the program avoids importing a function it does not need.
That is all today :D. If you fancy seeing my #100Days journey, please drop by