3.3 Requiring Your Own Module

  1. Create File notes.js in notes-node. This file will be later used to do notes manipulation, but you do not need to worry about it right now.
  2. Wirte console.log('Sarting notes.js') into notes.js
  3. In app.js, we add a contant variable to represent our notes.js module. We use relative path, typing const notes = require('./notes.js')
$ node app.js
starting app...
Sarting notes.js

We could see two lines coming from different modules. The first line is from our main module app.js, and the second line is from the module we just created, notes.js. It demonstrates a property that the root functions in a module will be executed when required.

  1. We have a built-in variable in every module, called just module. Let’s explore that variable in notes.js by adding console.log(module).
    If we run the app.js again, we would see the original two lines still at top but a lot more information about module variable showing up.
$ node app.js
starting app...
Sarting notes.js
Module {
id: 'C:\\Users\\GaoJu\\OneDrive - University of Waterloo\\CURRENT\\notes-node\\notes.js',
exports: {},
parent:
Module {
id: '.',
exports: {},
parent: null,
filename: 'C:\\Users\\GaoJu\\OneDrive - University of Waterloo\\CURRENT\\notes-node\\app.js',
loaded: false,
children: [ [Circular] ],
paths:
[ 'C:\\Users\\GaoJu\\OneDrive - University of Waterloo\\CURRENT\\notes-node\\node_modules',
'C:\\Users\\GaoJu\\OneDrive - University of Waterloo\\CURRENT\\node_modules',
'C:\\Users\\GaoJu\\OneDrive - University of Waterloo\\node_modules',
'C:\\Users\\GaoJu\\node_modules',
'C:\\Users\\node_modules',
'C:\\node_modules' ] },
filename: 'C:\\Users\\GaoJu\\OneDrive - University of Waterloo\\CURRENT\\notes-node\\notes.js',
loaded: false,
children: [],
paths:
[ 'C:\\Users\\GaoJu\\OneDrive - University of Waterloo\\CURRENT\\notes-node\\node_modules',
'C:\\Users\\GaoJu\\OneDrive - University of Waterloo\\CURRENT\\node_modules',
'C:\\Users\\GaoJu\\OneDrive - University of Waterloo\\node_modules',
'C:\\Users\\GaoJu\\node_modules',
'C:\\Users\\node_modules',
'C:\\node_modules' ] }

We do not need to pay much attention to all these fields, but one to be noted, exports is probably the most useful field in module. We will see its usage.

  1. We write module.exports.age = 19; in the module.
  2. We write fs.appendFileSync('greetings.txt', `Hello ${user.username}! You are ${notes.age}.`); in our app.
  3. We would see Hello GaoJu! You are 19. appended at the end of greetings.txt. Note that the number 19 is from the module notes.js.
  4. The real advantage is not to export static variables but functions! Let’s try to export two functions here. In notes.js, we write
module.exports.addNote = () => {
console.log('addNote');
return 'New note';
}

In app.js we add two lines:

var res = notes.addNote();
console.log(res);

and run our app. Then, we see the following:

$ node app.js
starting app...
Sarting notes.js
addNote
New note

This means we are successfully using functions from another module!

  1. The last challenge is to make a function called add in module. It takes two numbers and returns the sum of them. Inside our main app, we want to use it and log the result to the terminal. In the module, we define the function add:
module.exports.add = (a, b) => {
return a + b;
}

In app.js, we are going to use the add function by console.log('results:', notes.add(7, 19)). And by running our app, we will see the result printed to the screen:

$ node app.js
starting app...
Sarting notes.js
results: 26