{"id":1760,"date":"2024-02-07T14:59:49","date_gmt":"2024-02-07T14:59:49","guid":{"rendered":"https:\/\/www.devopsfreelancer.com\/blog\/?p=1760"},"modified":"2024-02-27T15:08:51","modified_gmt":"2024-02-27T15:08:51","slug":"explaination-of-module-in-node-js-with-example","status":"publish","type":"post","link":"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/","title":{"rendered":"Explaination of Module in Node Js with Example ?"},"content":{"rendered":"\n<p>What is Module in Node Js?<\/p>\n\n\n\n<p>In Node.js, a module is essentially a reusable piece of code that encapsulates related functionality. Modules help in organizing code into separate files or packages, making it easier to manage, reuse, and maintain. There are different types of modules in Node.js:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Creating a Module:<\/strong> To create a module, you simply define your functionality in a separate file. This file can contain variables, functions, classes, or any other JavaScript code. By default, everything defined in a file in Node.js is scoped to that file, meaning it&#8217;s not automatically accessible from other files unless explicitly exported.<\/li>\n\n\n\n<li><strong>Exporting from a Module:<\/strong> To make functionality available outside the module, you use the <code>module.exports<\/code> or <code>exports<\/code> object to export the desired variables, functions, or objects. This makes them accessible to other modules that <code>require<\/code> or import them.<\/li>\n\n\n\n<li><strong>Importing\/Requiring a Module:<\/strong> To use functionality from a module in another file, you use the <code>require()<\/code> function to import the module. This function returns the exported functionality from the module, allowing you to use it within your code.<\/li>\n<\/ol>\n\n\n\n<p>Example:- <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1st create math.js file and put below code<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><strong><b>function add(a,b){\n    return a + b;\n}\n\nfunction sub(a,b){\n    return a-b;\n}\nmodule.exports = {\n   add,sub\n}<\/b>\n<\/strong><\/code><\/pre>\n\n\n\n<p>Next to create hellow.js file and put below code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const math = require(\".\/math\");\r\n\nconsole.log(\"math value is \",math);\r\n\r<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Next to run below command <\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run start<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"526\" src=\"https:\/\/www.devopsfreelancer.com\/blog\/wp-content\/uploads\/2024\/02\/image-70-1024x526.png\" alt=\"\" class=\"wp-image-1761\" style=\"width:1041px;height:auto\" srcset=\"https:\/\/www.devopsfreelancer.com\/blog\/wp-content\/uploads\/2024\/02\/image-70-1024x526.png 1024w, https:\/\/www.devopsfreelancer.com\/blog\/wp-content\/uploads\/2024\/02\/image-70-300x154.png 300w, https:\/\/www.devopsfreelancer.com\/blog\/wp-content\/uploads\/2024\/02\/image-70-768x395.png 768w, https:\/\/www.devopsfreelancer.com\/blog\/wp-content\/uploads\/2024\/02\/image-70-440x226.png 440w, https:\/\/www.devopsfreelancer.com\/blog\/wp-content\/uploads\/2024\/02\/image-70.png 1366w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>What is Module in Node Js? In Node.js, a module is essentially a reusable piece of code that encapsulates related functionality. Modules help in organizing code into&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[691],"tags":[692,697,698],"class_list":["post-1760","post","type-post","status-publish","format-standard","hentry","category-nodejs","tag-nodejs","tag-nodejs-module","tag-what-is-module-in-node-js"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Explaination of Module in Node Js with Example ? - DevOps Freelancer<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Explaination of Module in Node Js with Example ? - DevOps Freelancer\" \/>\n<meta property=\"og:description\" content=\"What is Module in Node Js? In Node.js, a module is essentially a reusable piece of code that encapsulates related functionality. Modules help in organizing code into...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps Freelancer\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/amitsthakurs\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-07T14:59:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-27T15:08:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.devopsfreelancer.com\/blog\/wp-content\/uploads\/2024\/02\/image-70-1024x526.png\" \/>\n<meta name=\"author\" content=\"Amit Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/amits_thakurs\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Amit Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/explaination-of-module-in-node-js-with-example\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/explaination-of-module-in-node-js-with-example\\\/\"},\"author\":{\"name\":\"Amit Kumar\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"headline\":\"Explaination of Module in Node Js with Example ?\",\"datePublished\":\"2024-02-07T14:59:49+00:00\",\"dateModified\":\"2024-02-27T15:08:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/explaination-of-module-in-node-js-with-example\\\/\"},\"wordCount\":211,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/explaination-of-module-in-node-js-with-example\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/image-70-1024x526.png\",\"keywords\":[\"nodejs\",\"nodejs-module\",\"what-is-module-in-node-js\"],\"articleSection\":[\"nodejs\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/explaination-of-module-in-node-js-with-example\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/explaination-of-module-in-node-js-with-example\\\/\",\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/explaination-of-module-in-node-js-with-example\\\/\",\"name\":\"Explaination of Module in Node Js with Example ? - DevOps Freelancer\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/explaination-of-module-in-node-js-with-example\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/explaination-of-module-in-node-js-with-example\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/image-70-1024x526.png\",\"datePublished\":\"2024-02-07T14:59:49+00:00\",\"dateModified\":\"2024-02-27T15:08:51+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/explaination-of-module-in-node-js-with-example\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/explaination-of-module-in-node-js-with-example\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/explaination-of-module-in-node-js-with-example\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/image-70.png\",\"contentUrl\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/image-70.png\",\"width\":1366,\"height\":702},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/explaination-of-module-in-node-js-with-example\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Explaination of Module in Node Js with Example ?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/\",\"name\":\"DevOps Freelancer\",\"description\":\"We provide DevOps | SRE | DevSecOps | MLOps Freelancing\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\",\"name\":\"Amit Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"caption\":\"Amit Kumar\"},\"description\":\"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\\\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/amitsthakurs\\\/\",\"https:\\\/\\\/www.instagram.com\\\/amits_thakurs\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/amits-thakurs\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/amits_thakurs\"],\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/author\\\/amit\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Explaination of Module in Node Js with Example ? - DevOps Freelancer","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/","og_locale":"en_US","og_type":"article","og_title":"Explaination of Module in Node Js with Example ? - DevOps Freelancer","og_description":"What is Module in Node Js? In Node.js, a module is essentially a reusable piece of code that encapsulates related functionality. Modules help in organizing code into...","og_url":"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/","og_site_name":"DevOps Freelancer","article_author":"https:\/\/www.facebook.com\/amitsthakurs\/","article_published_time":"2024-02-07T14:59:49+00:00","article_modified_time":"2024-02-27T15:08:51+00:00","og_image":[{"url":"https:\/\/www.devopsfreelancer.com\/blog\/wp-content\/uploads\/2024\/02\/image-70-1024x526.png","type":"","width":"","height":""}],"author":"Amit Kumar","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/amits_thakurs","twitter_misc":{"Written by":"Amit Kumar","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/#article","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/"},"author":{"name":"Amit Kumar","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"headline":"Explaination of Module in Node Js with Example ?","datePublished":"2024-02-07T14:59:49+00:00","dateModified":"2024-02-27T15:08:51+00:00","mainEntityOfPage":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/"},"wordCount":211,"commentCount":0,"image":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.devopsfreelancer.com\/blog\/wp-content\/uploads\/2024\/02\/image-70-1024x526.png","keywords":["nodejs","nodejs-module","what-is-module-in-node-js"],"articleSection":["nodejs"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/","url":"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/","name":"Explaination of Module in Node Js with Example ? - DevOps Freelancer","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/#primaryimage"},"image":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.devopsfreelancer.com\/blog\/wp-content\/uploads\/2024\/02\/image-70-1024x526.png","datePublished":"2024-02-07T14:59:49+00:00","dateModified":"2024-02-27T15:08:51+00:00","author":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"breadcrumb":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/#primaryimage","url":"https:\/\/www.devopsfreelancer.com\/blog\/wp-content\/uploads\/2024\/02\/image-70.png","contentUrl":"https:\/\/www.devopsfreelancer.com\/blog\/wp-content\/uploads\/2024\/02\/image-70.png","width":1366,"height":702},{"@type":"BreadcrumbList","@id":"https:\/\/www.devopsfreelancer.com\/blog\/explaination-of-module-in-node-js-with-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.devopsfreelancer.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Explaination of Module in Node Js with Example ?"}]},{"@type":"WebSite","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website","url":"https:\/\/www.devopsfreelancer.com\/blog\/","name":"DevOps Freelancer","description":"We provide DevOps | SRE | DevSecOps | MLOps Freelancing","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopsfreelancer.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b","name":"Amit Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","caption":"Amit Kumar"},"description":"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.","sameAs":["https:\/\/www.facebook.com\/amitsthakurs\/","https:\/\/www.instagram.com\/amits_thakurs\/","https:\/\/www.linkedin.com\/in\/amits-thakurs\/","https:\/\/x.com\/https:\/\/twitter.com\/amits_thakurs"],"url":"https:\/\/www.devopsfreelancer.com\/blog\/author\/amit\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/comments?post=1760"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1760\/revisions"}],"predecessor-version":[{"id":1762,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1760\/revisions\/1762"}],"wp:attachment":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/media?parent=1760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/categories?post=1760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/tags?post=1760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}