Node modules-Node.js essential knowledge 2

Haifeng Wang
3 min readOct 13, 2020

The require function is what we use to load modules, many of the modules that we load and work with are hosted on NPM, and delivered by the community. But right now we only focus on those node modules that we don’t have to install with NPM, these modules are installed locally with the installation of Node.js. These modules we call them core modules.

const path = require("path");const util = require("util");const v8 = require("v8");

Readline is a module we can use to help us build an application that would ask questions of a terminal user.

const readline = require("readline");const rl = readline.createInterface({input: process.stdin,
output: process.stdout
})rl.question("How do you like Node? > ", answer => {
console.log(`Your Answer: ${answer}`);
})
const readline = require("readline");const rl = readline.createInterface({input: process.stdin,output: process.stdout})// Write a fileconst questions = ["What is your name?","what would you rather be doing?","What is your preferred programming language"]// The second argument is to be invoked when we're finishedconst collectAnswers = (questions, done) => {const answers = [];const [firstQuestion] = questions;const questionAnswered = answer => {answers.push[answer];if(answers.length < questions.length){rl.question(questions[answers.length], questionAnswered)}else{// Whenever we are done, we are going to invoke this argument, pass the answers backdone(answers)
}
}
// The first argument is the string, the second is a function that will be invoked once the question is answeredrl.question(firstQuestion, questionAnswered)}collectAnswers(questions, answers => {console.log("Thank you for your answers. ");console.log(answers);process.exit();})

Custom modules

How to export custom modules?

In Node.js, every Javascript file is its own module, we’ve been loading modules with the the require function, the require function is part of the common JS module pattern, but it only represents half the pattern, the other half of the pattern is module.exports, or the mechanism that we use to export data and functionality from a module.

Within every javascript file, all of the variables are scoped to that module, so the files that consume this module won’t have access to the count. Create myModule.js.

let count = 0;const inc = () => ++count;
const dec = () => --count;

getCount will return the current value of the count

const getCount = () => count;// export a module to app.js
module.exports = {
inc,
dec,
getCount
};

Then go to app.js

const { inc, dec, getCount } = require("./myModule");inc();
inc();
dec();
console.log(getCount());

How to create a module?

We can export a reusable function as a module, and organize the new module file in the module folder, and then use the require function to use that module.

Custom events with EventEmitter

Events module is a core module that comes with Node.js.

Create an instance of the Event Emitter. Events module comes with an Event Emitter constructor. We use emitter’s emit function to raise a custom event.

const events = require("events");
const emitter = new events.EventEmitter();
emitter.emit("customEvent", "Hello world", "computer");
emitter.emit("customEvent", "That's pretty cool", "Alex");

Using the emitter.on function to handle the custom event.

emitter.on("customEvent", (message, user) => {
console.log(`${user}: ${message}`);
});

One other important feature of the Event Emitter is that it’s asynchronous.It is a example by listening for user input from the terminal.

process.stdin.on("data", data => {const input = data.toString().trim();if (input === "exit") {emitter.emit("customEvent", "Goodbye", "process");process.exit();}emitter.emit("customEvent", input, "terminal");});

--

--