Node.js Tips Part 5

junior az
4 min readSep 27, 2020

For the previous parts of the Node’s Tips, we talked about the importance of knowing the basics of Node.js to be able to start a Node application. we talked about the right way to use events and callback functions also showed examples of using eventEmitter to print out messages. We Also talked about how Node has a built-in file system module called FS it can read, write and delete files on the file system among other things and it can also do other things in a blocking or non-blocking way.

Now we are going to talk about the modules & NPM. A module is nothing more than a JavaScript file that exports its code. Node has a bunch of built-in modules like FS and event that we already looked at in one of the previous articles. there is a long list of other modules beyond that, the traditional way to import a module on Node is to use this require function but a quick side note on that first because Node recently added support for Es MODULES which use the import and export syntax. Most Node.js code out there is written in vanilla JavaScript still uses require function so it is very important for your knowledge as Node.js developer and if all that is confusing to you well that is the JavaScript’s world.

Let’s look at how we can use modules in our own codebase. Create a new file to serve as your module then go into your index.js file create a variable for the module and then import it using require when you consle.log it you will notice that it is currently just an empty object.

In order to make a module useful, you need to export some code from it. In the modules file you can reference this object with module.exports you can add any properties to the object or redefine it as a new object in either case whatever you add here will now be available to use in the other file and as you can see in the images below the object is no longer empty when we Console.log it.

But at some point, you are very likely to want to use somebody else’s code out there in the world and the primary place to do that is via node package manager or NPM, Which is recently acquired by Github which itself was recently acquired by Microsoft.

When we installed node as we learned in the first part it also installed NPM which is a tool you can use to remote packages to use in your own code.

The first step we will take is to open the command line and run npm and it will the white flag to use the default options.

What you will notice is that it creates a package.json file in the root of the application and this file can contain metadata about your project but most importantly it keeps track of the dependencies that you used in the command line.

At this point, we have not installed anything yet so let us change that by opening the command line again and running npm install express.

Express is a minimal web application framework and one of the most popular third-party Node modules. After running the command you will see few things that happened in the package.jason file, it added Express to the dependencies and pegged to a specific version.

This dependencies object allows you to manage multiple dependencies in a project and reinstall them all at once on the different system. Now the actual raw source code for the dependency lives in the node modules directory.

To add as a note, you should never have to modify code in the node modules directory if you find yourself writing code in here you are probably doing it wrong.

That is because the package.json controls how this directory is built, it fetches your remote packages, saves the source code here and that process should be able to be repeated on any system.

In the last part of this article, we will see how we import packages by name and use the 7 steps together to build a real full-stack application.

--

--