Skip to main content

Command Palette

Search for a command to run...

Getting Started With Node Modules I

Understanding modules in JavaScript

Updated
β€’3 min readβ€’View as Markdown
Getting Started With Node Modules I
N

I'm a developer, who is keen about building solutions in the web. I love to read and write articles.

Hello there πŸ‘‹, This is the second article in the Node.js series.

In this article, we will be diving deeper into the node modules we have.

NB: If you are new to this series, here is a link to the previous article, Introduction to Node.js

What is a Module?

A module is a block of code that is contained and interacts with external applications based on shared functionality.

NB: A module can be a single file or a collection of files and folders. It can also be seen as a section of program functionality.

There are three types of modules in any of our applications:

  1. Core modules
  2. Local modules
  3. Third-party modules

Core Modules

These are in-built modules that come with the Node.js application. These modules don't need to be installed as they are "follow-come". The Node core modules can be seen here. Some common examples are the Events, HTTP, File system, OS etc.

To use any of these models, we either import them by using the import command:

import varName from "moduleName";
// or 
import { varName } from "moduleName";

or use the require keyword:

const varName = require('moduleName');

NB: When using the require keyword, it is advisable that the const for declaring that variable. This is because, we do not want to redeclare that variable again, thereby changing its meaning

Local Modules

These are files within our development environment. The modules are written by the developer, but needed in another file or module within the development environment. Let's say our files are arranged in this order

localmodules.png

If we need a particular functionality from the app.js file inside the index.js file which is inside the same folder "HASHNODE PROJECT", here is how we import it using the require keyword and our relative URL

const app = require('./app.js')

If we require this same functionality in the user.js file which is in the "model" folder,

const app = require('../app.js');

Third-Party Modules

These are modules that have been written by other developers, but you need them in your code. Node.js has a package manager for getting these modules and packages. This packager is called the Node Package Manager (NPM) and is the most popular. There are other package managers, for example :

To use the NPM to install a module,

npm install express
// or 
npm i expresss

After installing it, we can now require the module in any file we need it.

In the next article, I'll be talking about the NPM, so stay tuned.

Thanks for reading. πŸ™πŸ™

I encourage you to subscribe to my newsletter to get an update when a new article drops.

You can also follow me on these platforms:

Twitter 🐦: @nwokporo_ebuka

LinkedIn ⚑: @chukwuebuka_nwokporo

GitHub πŸš€: @ebukvick

Hashnode πŸ“—: @codedaddy

J

Nice piece Chukwuebuka. Thank you boss for this

4

NODEJS

Part 4 of 5

This series will help a developer understand everything he/she is supposed to know about using the Node-js runtime environment and other packages. This will also help in building simple projects.

Up next

Getting Started With Node.js

Setting up an Environment for Running Node.js