Node.js Module system Part 3( note-taking app)

CG_Musta
5 min readAug 27, 2020

Chalk, nodemon package & Node process.argv

In the second part of the node Module blog, we learned how to load the npm package in our application and use it. Now we are going to install some npm packages that will help us to start building our app.

chalk npm package

Chalk is a package that helps to customise the text that printed out in the console. If we are building a web server with Express, for example, this can help us to customise how we print errors in the console, for example, using red colour, or maybe using warning message in yellow.

First lets first install chalk in our application

npm install chalk@4.1.0

now we need to require chalk in our file and assign it to a variable as we learned in the previous blog.

Now we need to require the module in our file and assigned to a variable.

const chalk = require('chalk')

We need to test that the package works in our app now. We can call one of the functions that we find the documentation.

chalk.blue('Hello world')

Console log the result and see if in the terminal the word “Hello World” appears in blue. Suppose it does mean that our app works perfectly fine.

You can also chain some and nest styles, for example, if you want: blue text with a red background and bold style you can write:

chalk.blue.bgRed.bold('Hello world')

The order does not matter, and here are the results:

nodemon npm package

This module will be constructive for when we are making changes in our application and want to avoid restarting the server manually. The nodemon module does not require any changes in the code we need to run the application using the nodemon word instead of the Node when executing the application, and it will do everything automatically.

Installation:

npm install nodemon@2.0.4 -g

In the command above we added the “-g” command which stands for ‘Global”. When you install something globally, your package won’t change because it gets installed on your operating system itself and not to an individual project if you open the package.json you won’t see any changes. You do not require it in your file like with the other packages we installed.

Run nodemon:

first check if it was installed correctly, in the console run:

nodemon -v

Run the app.js:

nodemon app.js

All the changes you are making to the application will be automatically updated once you save the file without stopping and then rerunning the server.

To continue with our app, we going to focus on to main subjects, The file system which will allow us to store the notes data from the user, and our argument command line arguments are going to enable us to get the input from the user.

Get user input from the command line.

When we run the command “node app.js” to start our application, we can add additional value to send to the application, for example:

node app.js name

This command will run the same, but it sends an additional piece of information to our program. Our program can choose to use it in a dynamic way like printing a greeting with the name provided, but where exactly can we access this extra information?. The answer is that we can find this information in the global variable. Like javascript running in the browser has window and document, Node has global and process, and this is where we can access the command line arguments we passed in the process.

console.log(process.argv)

Process is a big object with dozens of different methods and properties. But the one we are looking for is “argv”, stands for argument vector and is an array containing all the argument provided. So let’s run this in the console and see the outcome.

We have a new array with three strings, and the first is the path to the node js on your machine, the second is the path to the file we are running, and the last one is the argument we provided, in this case, I passed my name. If we want to grasp this value now we can use the javascript index to select the 3rd array:

console.log(process.argv[2])

In this how we can select just the argument, we are interested in. Let’s see how we can now build some logic using the command passed into the console from the user. If we have a note application we would like to add or remove notes, in this case, we can provide the command “add” or “remove” to ask the application to do something.

In the example, I build a simple if statement that checks if the input of the user is equal to add or remove and print the value to the console. If the value passed is not recognised will print “command not recognised”, as you can see I saved the argument coming from the “process. argv” into a variable to make it easier to work with, and below I run all the command already to show you the results I will get for each argument passed. Of course, it is not enough to give the command, but we also need to tell for example to our app what to add or delete for example a title, and this is how we can achieve this:

node app.js add --title="your title here"

In our “process.argv” we can find the 4th argument now let’s see how does it look like:

Now we have all the information we need in our object, but you can notice that it is not phrased already for us, so we would need to build some code to do it for us. Fortunately, there is an npm package that can help us to do this job which is not relevant to our app so we can focus on the core of our programming. In the next blog, we will explore this package on how to extract that value and use it to build the note-taking app.

--

--