Creating and using our own modules
In this example, we use two modules, each corresponding to a JavaScript file:
- The first module (here named
test.js
) will be the main file of our application, the one we execute using thenode test.js
command in a command window. - The second module (here named
module1.js
) will be the one we want to use in our maintest.js
module. Themodule1.js
module will then be enriched to show how its functionalities are accessible outside the module (and will therefore be used in the maintest.js
module).
Let’s go ahead and create these two modules.
Creating a module
Here is the content of the two files, module1.js
and test.js
:
module1.js file
console.log("module1.js is loaded");
The module currently has a simple console.log()
statement. The module will then be enriched. The main module test.js is the following:
test.js file
var mod1 = require("./module1.js"); // or require("./module1...