Node.js How Modules Works & NPM 02

Video Tutorial

Overview

In the last tutorial we have covered the basic setup for running a node js application also talked about REPL and why it is very useful for you since you are going to develop apps using node js, so in this tutorial we will see how modules work so you can freely use it on your project also how and why to use third-party modules made by the community for you to speed up your work progress.

NPM

NPM stands for node package manager, which basically let you manage your modules/package also download and use third-party modules which are register on the npm registry, just access the NPM repository then you can search for any package you need.

So to install any package into your local dev environment make sure you are on the root dir of your project, and npm install package_name

# so we just install the request package, very basic HTTP Request/Response 
npm install request --save 
#--save to save it as a realtime dependency or --save-dev for development only

And you can save the package either as a real-time dependency which means, it has to present in order for your app to run (like doing HTTP Request) or only for development

— save for real-time dependency

— save-dev for development only dependency

It very simple to install packages, and it only going to take a couple of seconds to complete the package installation process.

Rather than installing packager NPM Also can allow you to define custom scripts then constantly run them for speeding up the dev process for ex (run your app using node), we will cover that more in deep when we try to make a basi HTTP Server using Node.js.

How Modules Works

So modules basically are js files that provide you with methods that you need outside of the currently defined in file, pretty much like requiring, you can benefit from that by using it on your app in order to speed up the dev process also for easier dependency management, to briefly understand that let’s create a new file under our root dir name it utils.js where it is just going to hold a variable has a Hello World sentence on it and a method to return this variable, very basic one actually

let name = 'Hello World';
//We use the exports object for exporting the method from one module to another 
exports.getName = function getName() {
    return name;
}

Now if we would get the name variable content we just need to call the getName method from the main.js file but before we need to import the utils.js file, for that we use the require() method

/* main.js */
utils = require('utils.js');
//Since we are exporting the getName method then we can call it from the utiles object
console.log(utils.getName());
//We should See : "Hello World" Printed!

Require method returns an object that has the methods, variable, objects… we exported from the module (in this case utils.js file), so you can exchange data between different files with the help of node.js.

Also, you can use require() in order to import installed packages (Third Party packages) after installing them using NPM.

Now you can either install third-party packages that can help during the dev process while npm has a very large set of packages also benefit from modules like files in your project for easier integration.

What’s Next

So that was the basic idea behind modules on node.js and how they can play a major role in your app, besides that will help you understand the more complicated apps using node.js that we will cover in the next tutorials.

No Comments Yet