DEV Community

Cover image for Getting to Know Node.js (Part II)
Oscar Luna
Oscar Luna

Posted on

Getting to Know Node.js (Part II)

Hello again! Last time we started looking into Node.js, and some basics of running Node on the command line. We also touched on how Node.js works under the hood. If you missed the first part of this series about Node.js you can catch up with Part I here.

Node has a module system built with Common.js where we can request modules that are either built-in or downloaded using the function require. The require function receives the path of the desired module, relative to the current module the request is being made from. We use ./ to indicate that our required module is in the current directory, and ../ to indicates that it is one directory up from the current one. We also use /, but to indicate the root of our file system.

Say you have a directory with the files HeaderComponent.js and HeaderContainer.js inside it, and we need to import HeaderComponent.js into HeaderContainer.js. Since they are in the same directory, we request the module with const HeaderContainer = require('./HeaderContainer');. We can omit .js from the pathname and Node knows to append it to the filename when importing the module. With Node we can also request built-in modules such as fs, which can be found in Node's node_modules directory. To do so, we simply omit the pathname syntax altogether like so: const fs = require('fs');. Modules that get installed to the node_modules directory also follows this syntax, but more on that later. For now, it's important to know the proper syntax when requesting our own files versus built-in directories.

Let's say I wanted to write a program that takes a string s and identifies whether it is a palindrome (written the same way as it would be if reversed). We can start with a file main.js that will contain our command line script.

//main.js
const PalindromeCheck = require('./PalindromeCheck');

//Index 2 in process.argv holds the first *actual* command line argument
let arguments = process.argv[2];

console.log(PalindromeCheck(argument)); 

--

//PalindromeCheck.js
exports.PalindromeCheck = function(s, i) {
 return(i=i||0)<0||i>=s.length>>1||s[i]==s[s.length-1-i]&&PalindromeCheck(s,++i);
}

Enter fullscreen mode Exit fullscreen mode

By adding a property PalindromeCheck to exports we add it to the module's interface, so NodeJS can treat the file as a module for main.js to import PalindromeCheck.js and use its function. We can then go back to the command line and run:

$node main.js racecar
//racecar
Enter fullscreen mode Exit fullscreen mode

Built-in modules aren't the only thing that can be imported from Node. Next time, we will discuss further on importing modules and how Node.js gives us access to (pinky to lip) millions of modules to import, and, in my opinion, this is where the process of writing code gets exciting for me. So stick around because I'll be discussing, at last, the Node Package Manager on Part III. See ya soon!


Works Cited

  1. Haverbeke, Martin "Node.js", Eloquent JavaScript - A Modern Introduction to Programming, 3rd Edition, 2019, Chapter 20, No Starch Press, Inc.
  2. NodeJS Foundation. "Introduction to Node.JS" Node.js. Accessed November 13, 2020. https://nodejs.dev

Top comments (0)