{"id":72669,"date":"2020-01-14T16:40:09","date_gmt":"2020-01-14T11:10:09","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=72669"},"modified":"2024-04-12T14:22:59","modified_gmt":"2024-04-12T08:52:59","slug":"the-module-wrapper","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/the-module-wrapper\/","title":{"rendered":"The module wrapper"},"content":{"rendered":"<p>Before a module&#8217;s code is executed, Node.js will wrap it with a function wrapper that looks like the following:<\/p>\n<p>(function(exports, require, module, __filename, __dirname) {<\/p>\n<p>\/\/ Module code actually lives in here<\/p>\n<p>});<\/p>\n<p>By doing this, Node.js achieves a few things:<\/p>\n<ul>\n<li>It keeps top-level variables (defined with var, const or let) scoped to the module rather than the global object.<\/li>\n<li>It helps to provide some global-looking variables that are actually specific to the module, such as:<\/li>\n<li>The module and exports objects that the implementor can use to export values from the module.<\/li>\n<li>The convenience variables __filename and __dirname, containing the module&#8217;s absolute filename and directory path.<\/li>\n<\/ul>\n<p>__dirname<\/p>\n<p>Added in: v0.1.27. It is &lt;string&gt;<\/p>\n<p>The directory name of the current module. This is the same as the path.dirname() of the __filename. Example: running node example.js from \/Users\/mjr<\/p>\n<p>console.log(__dirname);<\/p>\n<p>\/\/ Prints: \/Users\/mjr<\/p>\n<p>console.log(path.dirname(__filename));<\/p>\n<p>\/\/ Prints: \/Users\/mjr<\/p>\n<p>__filename<\/p>\n<p>Added in: v0.0.1 It is &lt;string&gt;<\/p>\n<p>The file name of the current module. This is the current module file&#8217;s absolute path with symlinks resolved. For a main program this is not necessarily the same as the file name used in the command line.<\/p>\n<p>Examples:<\/p>\n<p>Running node example.js from \/Users\/mjr<\/p>\n<p>console.log(__filename);<\/p>\n<p>\/\/ Prints: \/Users\/mjr\/example.js<\/p>\n<p>console.log(__dirname);<\/p>\n<p>\/\/ Prints: \/Users\/mjr<\/p>\n<p>Given two modules: a and b, where b is a dependency of a and there is a directory structure of:<\/p>\n<ul>\n<li>\/Users\/mjr\/app\/a.js<\/li>\n<li>\/Users\/mjr\/app\/node_modules\/b\/b.js<\/li>\n<\/ul>\n<p>References to __filename within b.js will return \/Users\/mjr\/app\/node_modules\/b\/b.js while references to __filename within a.js will return \/Users\/mjr\/app\/a.js.<\/p>\n<h3>exports<\/h3>\n<p>Added in: v0.1.12 It is &lt;Object&gt;. A reference to the module.exports that is shorter to type. See the section about the exports shortcut for details on when to use exports and when to use module.exports.<\/p>\n<h3>module<\/h3>\n<p>Added in: v0.1.16 It is &lt;module&gt;. A reference to the current module, see the section about the module object. In particular, module.exports is used for defining what a module exports and makes available through require().<\/p>\n<p>require(id)<\/p>\n<p>Added in: v0.1.13 It is id &lt;string&gt; module name or path. It returns, &lt;any&gt; exported module content. Used to import modules, JSON, and local files. Modules can be imported from node_modules. Local modules and JSON files can be imported using a relative path (e.g. .\/, .\/foo, .\/bar\/baz, ..\/foo) that will be resolved against the directory named by __dirname (if defined) or the current working directory.<\/p>\n<p>\/\/ Importing a local module:<\/p>\n<p>const myLocalModule = require(&#8216;.\/path\/myLocalModule&#8217;);<\/p>\n<p>\/\/ Importing a JSON file:<\/p>\n<p>const jsonData = require(&#8216;.\/path\/filename.json&#8217;);<\/p>\n<p>\/\/ Importing a module from node_modules or Node.js built-in module:<\/p>\n<p>const crypto = require(&#8216;crypto&#8217;);<\/p>\n<p><strong>require.cache &#8211; <\/strong>Added in: v0.3.0 It is &lt;Object&gt;. Modules are cached in this object when they are required. By deleting a key value from this object, the next require will reload the module. Note that this does not apply to native addons, for which reloading will result in an error.<\/p>\n<p><strong>require.main &#8211; <\/strong>Added in: v0.1.17 It is &lt;module&gt;. The Module object representing the entry script loaded when the Node.js process launched. See &#8220;Accessing the main module&#8221;. In entry.js script:<\/p>\n<p>console.log(require.main);<\/p>\n<p>node entry.js<\/p>\n<p>Module {<\/p>\n<p>id: &#8216;.&#8217;,<\/p>\n<p>exports: {},<\/p>\n<p>parent: null,<\/p>\n<p>filename: &#8216;\/absolute\/path\/to\/entry.js&#8217;,<\/p>\n<p>loaded: false,<\/p>\n<p>children: [],<\/p>\n<p>paths:<\/p>\n[ &#8216;\/absolute\/path\/to\/node_modules&#8217;,<\/p>\n<p>&#8216;\/absolute\/path\/node_modules&#8217;,<\/p>\n<p>&#8216;\/absolute\/node_modules&#8217;,<\/p>\n<p>&#8216;\/node_modules&#8217; ] }<\/p>\n<p><strong>require.resolve(request[, options]) &#8211; <\/strong>Updated in v8.9.0. The \u00a0request is &lt;string&gt; for module path to resolve and options is &lt;Object&gt;, as<\/p>\n<ul>\n<li>paths &lt;string[]&gt; Paths to resolve module location from. If present, these paths are used instead of the default resolution paths, with the exception of GLOBAL_FOLDERS like $HOME\/.node_modules, which are always included. Note that each of these paths is used as a starting point for the module resolution algorithm, meaning that the node_modules hierarchy is checked from this location.<\/li>\n<\/ul>\n<p>It returns &lt;string&gt;. Use the internal require() machinery to look up the location of a module, but rather than loading the module, just return the resolved filename.<\/p>\n<p><strong>\u00a0<\/strong><strong>require.resolve.paths(request) &#8211; <\/strong>Added in: v8.9.0<\/p>\n<ul>\n<li>request &lt;string&gt; The module path whose lookup paths are being retrieved.<\/li>\n<li>Returns: &lt;string[]&gt; | &lt;null&gt;<\/li>\n<\/ul>\n<p>It returns an array containing the paths searched during resolution of request or null if the request string references a core module, for example http or fs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before a module&#8217;s code is executed, Node.js will wrap it with a function wrapper that looks like the following: (function(exports, require, module, __filename, __dirname) { \/\/ Module code actually lives in here }); By doing this, Node.js achieves a few things: It keeps top-level variables (defined with var, const or let) scoped to the module&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8453],"tags":[8481],"class_list":["post-72669","page","type-page","status-publish","hentry","category-node-js","tag-the-module-wrapper"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The module wrapper - Tutorial<\/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.vskills.in\/certification\/tutorial\/the-module-wrapper\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The module wrapper - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Before a module&#8217;s code is executed, Node.js will wrap it with a function wrapper that looks like the following: (function(exports, require, module, __filename, __dirname) { \/\/ Module code actually lives in here }); By doing this, Node.js achieves a few things: It keeps top-level variables (defined with var, const or let) scoped to the module...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/the-module-wrapper\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:52:59+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/the-module-wrapper\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/the-module-wrapper\/\",\"name\":\"The module wrapper - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-14T11:10:09+00:00\",\"dateModified\":\"2024-04-12T08:52:59+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/the-module-wrapper\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/the-module-wrapper\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/the-module-wrapper\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The module wrapper\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The module wrapper - Tutorial","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.vskills.in\/certification\/tutorial\/the-module-wrapper\/","og_locale":"en_US","og_type":"article","og_title":"The module wrapper - Tutorial","og_description":"Before a module&#8217;s code is executed, Node.js will wrap it with a function wrapper that looks like the following: (function(exports, require, module, __filename, __dirname) { \/\/ Module code actually lives in here }); By doing this, Node.js achieves a few things: It keeps top-level variables (defined with var, const or let) scoped to the module...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/the-module-wrapper\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:52:59+00:00","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/the-module-wrapper\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/the-module-wrapper\/","name":"The module wrapper - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-14T11:10:09+00:00","dateModified":"2024-04-12T08:52:59+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/the-module-wrapper\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/the-module-wrapper\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/the-module-wrapper\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"The module wrapper"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/72669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=72669"}],"version-history":[{"count":3,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/72669\/revisions"}],"predecessor-version":[{"id":73183,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/72669\/revisions\/73183"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=72669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=72669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=72669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}