{"id":15941,"date":"2024-01-27T21:14:13","date_gmt":"2024-01-27T15:44:13","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=15941"},"modified":"2024-01-27T21:23:54","modified_gmt":"2024-01-27T15:53:54","slug":"nodejs-modules","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/nodejs-modules\/","title":{"rendered":"Node.js Modules: Core, Local and Third Party"},"content":{"rendered":"\n<p>Modules are the most important part of developing a Node.js application. Many beginners think of modules as they are external packages that can be installed using package managers like NPM to get some functionality but that&#8217;s not it. The modules we usually talk about are called third-party modules, besides them, there are other types of modules as well.<\/p>\n\n\n\n<p>Even Node.js lets us create our own modules to make our coding life easy by organizing complex functionality in single or multiple files of JavaScript that can be reused. These are called local modules that consist of code that performs specific activities and can be used later so that we do not need to code that functionality every time.<\/p>\n\n\n\n<p>Let&#8217;s dive deeper into the concept of modules, discover their types and understand how to use them properly to increase development output and skills in Node.js.<\/p>\n\n\n\n<p><strong><em>Also Read:<\/em><\/strong><a href=\"https:\/\/codeforgeek.com\/what-is-node-js\/\"><strong><em> What is Node.js?<\/em><\/strong><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Modules in Node.js<\/h2>\n\n\n\n<p>We can divide Node modules into three types based on how they are offered or how they interact with the application.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Core Modules<\/li>\n\n\n\n<li>Local Modules<\/li>\n\n\n\n<li>Third-Party Modules<\/li>\n<\/ol>\n\n\n\n<p>Let&#8217;s look at each of them one by one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Node.js Core Modules<\/h2>\n\n\n\n<p>Node.js Core Modules are the modules pre-built with Node.js. These are pre-written in the Node.js system and whenever you install Node.js they will be enclosed as part of the installation.<\/p>\n\n\n\n<p><strong>Some of the most popular core modules are:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/codeforgeek.com\/nodejs-web-module\/\">HTTP: Used for making all types of HTTP requests such as GET, POST, PATCH, DELETE, etc<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/codeforgeek.com\/node-js-file-system\/\">FS: A File System Module is used for working with files present in the operating system.<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/codeforgeek.com\/nodejs-path-module\/\">Path: It can be used to work with file paths.<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/codeforgeek.com\/nodejs-child-process\/\">Child Process: If you need to code on the operating system\u2019s process level, you can use this module.<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Using Node.js Built-in Modules <\/h3>\n\n\n\n<p>To use the core module it is not necessary to install it, you just need to import it. You can simply use the <strong>require() <\/strong>function and pass in that module\u2019s name. The <strong>require() <\/strong>function will return the module and save it in a variable.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst path = require(&#039;path&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst path = require(&#039;path&#039;)\n \nconst name = &#039;Sushant&#039;\nconst output = path.join(&#039;\/&#039;, &#039;Users&#039;, name, &#039;test.txt&#039;) \/\/&#039;\/Users\/Sushant\/test.txt&#039;\n \nconsole.log(output)\n<\/pre><\/div>\n\n\n<p>Here we have imported the \u201cpath\u201d module, we have not installed it because it is one of the Node.js core modules. Then we used its method join() to combine multiple path segments into one and console log the output.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"647\" height=\"102\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/01\/Node.js-Core-Modules.png\" alt=\"Output of Node Core Modules\" class=\"wp-image-26271\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/01\/Node.js-Core-Modules.png 647w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/01\/Node.js-Core-Modules-300x47.png 300w\" sizes=\"(max-width: 647px) 100vw, 647px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Node.js Local Modules<\/h2>\n\n\n\n<p>Sometimes developer needs the same functionality in different places in their software so they create their own local modules. <strong>Local Modules <\/strong>are those modules that are created by the developer and are not pre-built in Node.js. They are used to make development life more effortless and fast because they automatically decrease code length. Let\u2019s create our own local module.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating and Using Local Modules in Node.js<\/h3>\n\n\n\n<p>To use a local module, we first have to create it. Below is an example of creating and using a local module.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Let&#8217;s create a local module &#8220;greetingsModule.js&#8221; and then try to import and use it inside the main server file.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/ greetingsModule.js\n\/\/ Simple Greetings Module\n\nexports.sayGoodMorning = function(name){\n    console.log(`Good Morning ${name}!`)\n}\n\nexports.sayGoodAfternoon = function(name){\n    console.log(`Good Afternoon ${name}`)\n}\n\nexports.sayGoodNight = function(name){\n    console.log(`Good Night ${name}`)\n}\n<\/pre><\/div>\n\n\n<p>In the above code, we have created three functions and exported them using the exports keyword. This keyword will export these functions so that they can be imported to use in other files as modules.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst greetingsModule = require(&#039;.\/greetingsModule&#039;)\n\ngreetingsModule.sayGoodMorning(&#039;Sushant&#039;)\ngreetingsModule.sayGoodAfternoon(&#039;Sushant&#039;)\ngreetingsModule.sayGoodNight(&#039;Sushant&#039;)\n<\/pre><\/div>\n\n\n<p>Here, we have imported the file created in the above step using the <strong>require()<\/strong> method. Then used the dot operator and called all three functions. The output of all three functions can be shown below.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"612\" height=\"152\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/01\/Node.js-Local-Modules.png\" alt=\"Output of Node Local Modules\" class=\"wp-image-26272\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/01\/Node.js-Local-Modules.png 612w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/01\/Node.js-Local-Modules-300x75.png 300w\" sizes=\"(max-width: 612px) 100vw, 612px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Node.js Third-Party Modules<\/h2>\n\n\n\n<p>Most of the time coding requires several kinds of functionality. Now to do this we have two methods. The first method is we can implement complete functionality on our own and the second method is we can use any third-party module which will provide us with the same functionality. We usually go for the second option. The 3rd party modules are libraries that are publicly available and developed by someone else. These are found on repositories such as<a href=\"https:\/\/codeforgeek.com\/nodejs-npm-node-package-manager\/\"> <strong>npm<\/strong><\/a>.<\/p>\n\n\n\n<p><strong>Some of the most popular third-party modules are: <\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.npmjs.com\/package\/mongoose\" target=\"_blank\" rel=\"noopener\">Mongoose<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.npmjs.com\/package\/multer\" target=\"_blank\" rel=\"noopener\">Multer<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.npmjs.com\/package\/body-parser\" target=\"_blank\" rel=\"noopener\">Body Parser<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/codeforgeek.com\/jwt-authentication-api-in-node-js\/\" data-type=\"URL\" data-id=\"https:\/\/codeforgeek.com\/jwt-authentication-api-in-node-js\/\" target=\"_blank\" rel=\"noreferrer noopener\">JSON Web Token<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Installing and Using Third-Party Modules with NPM<\/h3>\n\n\n\n<p>To use third-party NodeJS modules, it is necessary to install them first. To install any third-party module we need a package manager. The most popular package manager is<a href=\"https:\/\/codeforgeek.com\/nodejs-npm-node-package-manager\/\"> <strong>npm<\/strong><\/a>. When you install Node.js in your system, npm is also automatically installed.<\/p>\n\n\n\n<p>Below is the command to install any package from npm directly into your project.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nnpm install express\n<\/pre><\/div>\n\n\n<p>The above command will install a module<strong> \u201c<\/strong>express<strong>\u201d<\/strong> in your project.<\/p>\n\n\n\n<p>Also, we can install the module globally so that it can be accessed by any of your projects, you can use the same command with an additional attribute <strong>\u201c-g\u201d<\/strong> to install a module globally.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nnpm install -g express\n<\/pre><\/div>\n\n\n<p><strong>Here -g means global installation.<\/strong><\/p>\n\n\n\n<p>After installing a third-party module locally or globally, you can import it into your project using <strong>require()<\/strong>, just like we imported the core module or a local module.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst express = require(&#039;express&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Let&#8217;s use the express module and create a simple Node.js application that sends an HTML file for the home page containing an h1 tag with the message \u201cHome\u201d.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst express = require(&#039;express&#039;);\n \nconst app = express();\n \napp.get(&#039;\/&#039;, (req, res) =&gt; {\n    res.sendFile(__dirname + &#039;\/index.html&#039;);\n});\n \napp.listen(3000);\n<\/pre><\/div>\n\n\n<p>Here we have imported the \u201cexpress\u201d module that we already installed. Then we used its \u201csendFile()\u201d method to load and send an HTML file to the client when the request was received for the home(\u201c\/\u201d) route.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"978\" height=\"527\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/01\/Node.js-Third-Party-Modules.webp\" alt=\"Output of Node Third Party Modules\" class=\"wp-image-26273\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/01\/Node.js-Third-Party-Modules.webp 978w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/01\/Node.js-Third-Party-Modules-300x162.webp 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/01\/Node.js-Third-Party-Modules-768x414.webp 768w\" sizes=\"(max-width: 978px) 100vw, 978px\" \/><\/figure>\n\n\n\n<p><strong>Read More:<\/strong><a href=\"https:\/\/codeforgeek.com\/send-an-html-file-node-js\/\"><strong> <\/strong>How to send an HTML File in Node.js?<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>Modules are an essential part of a developer\u2019s life as they are very powerful and make things easy. There are three types of modules in Node JS, the core module which is a built-in module that comes with Node itself, a local module that can be created manually and a third-party module. These are very helpful if you want to save time. After reading this post, you are aware of modules, their types, use cases, and how you can install third-party modules. Thank you for reading this article, we hope it was helpful for you.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/nodejs.org\/api\/modules.html\" target=\"_blank\" rel=\"noopener\">https:\/\/nodejs.org\/api\/modules.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Modules are the most important part of developing a Node.js application. Many beginners think of modules as they are external packages that can be installed using package managers like NPM to get some functionality but that&#8217;s not it. The modules we usually talk about are called third-party modules, besides them, there are other types of [&hellip;]<\/p>\n","protected":false},"author":85,"featured_media":26275,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[14],"tags":[],"class_list":["post-15941","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nodejs"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/01\/Node.js-Modules.png",1280,720,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/01\/Node.js-Modules-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/01\/Node.js-Modules-300x169.png",300,169,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/01\/Node.js-Modules-768x432.png",768,432,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/01\/Node.js-Modules-1024x576.png",1024,576,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/01\/Node.js-Modules.png",1280,720,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/01\/Node.js-Modules.png",1280,720,false]},"uagb_author_info":{"display_name":"Sushant Dhiman","author_link":"https:\/\/codeforgeek.com\/author\/sushant\/"},"uagb_comment_info":0,"uagb_excerpt":"Modules are the most important part of developing a Node.js application. Many beginners think of modules as they are external packages that can be installed using package managers like NPM to get some functionality but that&#8217;s not it. The modules we usually talk about are called third-party modules, besides them, there are other types of&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/15941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/85"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=15941"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/15941\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/26275"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=15941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=15941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=15941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}