Skip to main content

Node.js Modules

Learn Node.js
Hello Folks! Today we will will move ahead and learn node.js more. We are going to write our first code in node.js today. Then we will discuss about modules in node.js. This post will be long so please stay tuned and let's code together.

Initializing the working directory:

Open a suitable text editor of your choice. I will use VS Code for the whole series of Node.js to learn Node.js.
From FILE, select OPEN FOLDER and select a working directory for your learning.
The alternative to this is, right click on the desired working directory and select OPEN WITH CODE.
After the working directory has opened we will have to initialize the directory for our Node.js activities.
We do it by :
>> npm init
After that a series of questions are asked as shown in the following picture :
use of npm init
We are asked package name, version, description, entry point, test command, git repository,keywords, author and license of our project.
Don't worry. Here I have left it empty for your proper understanding but I will surely discuss about every questions asked here.
package name is the name of the current working directory. version is about the version of your project. This uses Semantic versioning scheme. In short semantic versioning uses MAJOR.MINOR.PATCH way of defining a version for our project. By default it's always 1.0.0 which says it is the first version we are writing. In the description field you have to write few lines about the project, what it is all about. entry point is a very important fact. It defines the point from where the node runtime instance starts executing our code. test command is used to define your own custom test command. We will use this in later posts. git repository defines which git repository we will like to use. You can leave this field empty if you want to initialize a new git repository for this project at the end of coding. In the author field, write your name. In the license field, you can provide the license of your project or can leave it ISC, as it is.
Then when your command line asks,  "Is this OK?" type yes and press ENTER.
Congratulations, our working directory has been initialized. 
In this way we install package.json for our project.
Remember its npm init and not npm package.json
After this has been done, your will notice a file name package.json. If you open that file you will notice the details about your project in it.
package.json

So now our working directory is ready to execute node.js codes.

Our first Node.js script :

Let's open a new file and name it as index.js.
As I have told Node.js allows us to write JavaScript codes outside our browser, we will  write some JS codes to test this fact.
First node.js code
In the index.js file write "console.log("Hello World!")". Now we have to execute this code. 
Any guesses how will we do that?
In the terminal write the keyword "node", followed by the filename as shown in the picture above.
You will find the code has got executed and in the console it prints Hello World!

NOTE : If your terminal says that node is not recognized as an internal or externalcommand, check the process when you added node.exe to your path. Visit : Download Node.js for Windows

Working with JS classes in Node.js

Now we will implement JavaScript classes here. Create a new file, person.js and write the following code: 
Working with JS classes
Here, we are defining a class Person. We are using a constructor to initialize space for the object which we will create. The constructor will take 2 parameters, name and age. We have defined a method greeting() which will print the given sentence in the console.
Now in the index.js file comment out the previous code and write the following code:
Use of require()
Here, we are requiring the person.js file in this file by require("./person"). Unlike React.js we can't use import here. Then we are creating an object of the person class and calling the method greeting() over it.
Run the code by :
Console.log in node.js

See the sentence has been logged into the console.

(If you have any problem in JavaScript, let me know in the comment section. Then I will start a blog on JavaScript also.)

Modules in Node.js

While learning Node.js we find 3 types of modules.
They are:
  • Core Modules
  • Third Party Modules
  • User Defined Modules
First we will talk about the Core modules.

PATH Module:

First we will have to require the PATH Module.
const path = require("path"):
Then we will make the best use of path module.
learning PATH module
path.basename(__filename) gives us the name of the basefile name.
learning PATH module
path.dirname(__filename) gives us the absolte path in which our working file lies.
learning PATH module
path.extname(__filename) gives us the extension of the file. It defines what sought of file is that.
learning PATH module
path.parse(__filename) give us a JSON object which says everything about the working file we need to know. It is used to create path object.
learning PATH module
path.join() is used to concatenate paths.

This has been a really long blog. In the next post, we will discuss about FILE SYSTEM Module, OS Module, URL Module and many more.
If you are liking my posts please do share with your friends.
And I request everyone to provide your valuable feedback in the comment section as it lets me to improve.

Comments

Post a Comment

Popular Posts

Introduction to Node.js

Introduction to Node.js Hello Folks! This blog is for the new Node.js enthusiasts who wants to learn node.js . Here you will learn node.js online from me and you will learn node.js for free . I will guide on how to learn node.js and will help you to learn node.js from scratch. Prerequisites:  Welcome to the world of Back-end web development. Throughout this blog posts, I will guide you to become a backend developer . Before we dive deep into the topic, let us brush up the prerequisites. A website has 2 parts, the "Frontend" and the "Backend" . The Front-end of a website generally refers to the side of the website with which the user interacts. It's the User Interface(U.I.)  of the website. In simple terms, it consists of all those elements which we see and interact with after the web page loads in our computer. It is sometimes also known as "Client-side" of the web page. The Backend web development refers to the server-...

More about Node.js

More about Node.js Hello Folks! Welcome to the part 2 of the Introduction to Node.js blog. Before you read this blog further, it is must to read the previous blogs for better understanding. Here is the  Introduction to Node.js  link. So let's proceed to learn node.js . In this post we will mainly discuss on node.js architecture, the event loop and the thread pool.  Node.js Architecture: As said before, Node.js is  majorly dependent upon the Chrome's V8 engine and LIBUV library for its working. Now I guess you know what is V8 Engine and LIBUV library from the Part 1 of this blog. It is to be mentioned that LIBUV is a library in Node.js which is written in C++ and V8 Engine also uses C++ besides using JavaScript. Therefore, Node.js is not just JavaScript as you might have thought. Now, its time for us to discuss in detail the "Event Loop" and the "Thread Pool" in Node.js. The Event Loop enables Node.js to perform non-blocking I/O operat...

Download Node.js for Windows

Hello Folks! If you have not read the previous two blogs, I will highly encourage you to go and read them, before you start with this. Introduction to Node.js More about Node.js We have learned a lot of theories and now its time to get our hands dirty. We will download node.js in this post. Though we will again discuss some theories in the later posts, in this blog I will teach you, how to download node.js and configure your computer to follow the later blogs. I will discuss it in step by step and I will highly recommend you to follow the steps with me. Step -1: First visit the website :  https://nodejs.org/en/   Something like this will be shown : See, by default as my PC has Windows 8.1 and it has 64 bit architecture so I can download from  here. Major  Node .  js versions  enter  Current  release status for six months, which helps library authors time to contribute.  LTS  release status is "long-term supp...