Node.js is an open-source environment to run JavaScript outside the browser which is built on Chrome’s V8 JavaScript engine by Ryan Dahl. Also, Deno is another JavaScript runtime environment developed by the same person (But we are not going to talk about that here). Node.js is used to build highly scalable server-side applications with asynchronous I/O features. Here we are going to discuss built-in modules in Node.js and their uses.
What are Node.js modules?
We can refer to modules as small encapsulated units which can be reused and shared with anyone. Easier to maintain and debug because they are separated pieces of code from each other. In Node.js, each module has its own context, which stops it from overlapping with other modules and global scope. Each module can also be stored in its own JavaScript file in its own folder. Then import that file when we need to use that module.
Node.js modules can be categorized into three types as core modules, user-created or local modules, and third-party modules.
- Core modules — Inbuilt modules in the node.js
- User-created modules — Modules created by the user
- Third-party modules — Shared modules created and maintained by others
Core modules in Node.js
Basically, core modules are modules that are built into node.js these modules come automatically with the node.js installation. when you creating a node project these modules are available to use without any extra installations. before using core modules we need to import those modules into our project files using require keyword and I will mention how to import, use core modules in the next section.
There are lots of core modules out there. Here, I am going to discuss a few of the most important and most used among node.js core modules.
OS module
The os module provide methods and properties related to the current operating system.
Also, there are some other methods and properties to read about all the information, you can checkout os module documentation page here.
File System module
The file system module opens the doors to interacting with the file system of your device. In this scenario, we will see how to read from a file and how to write to a file using the file system module. To read and write there are two types of methods asynchronous/non-blocking way which we will be discussed and synchronous/blocking way. If you want to look into the synchronous way and other file system functionalities, check out this documentation page.
HTTP module
HTTP is one of the powerful modules in the node.js world that we use in networking applications. We can create a server that listens to HTTP requests in a certain port with this functionality we can create the backend for our application. Here, I will be creating a server that listens to a given port number.
In the browser, if you type localhost:3000 as URL Node.js is awesome will be displayed as the response. Also, there is much more to HTTP than this which you can find out using node.js HTTP documentation.
Top comments (0)