{"componentChunkName":"component---src-templates-blog-post-js","path":"/JavaScript/javascript-concept-12---commonjs/","webpackCompilationHash":"f92dbf25810602c1889e","result":{"data":{"site":{"siteMetadata":{"title":"Web Devlog","author":"RossenaHuh","siteUrl":"https://github.com/rossenahuh","comment":{"disqusShortName":"RossenaHuh","utterances":"rossenahuh/my-dev-blog.git"},"sponsor":{"buyMeACoffeeId":"rossena"}}},"markdownRemark":{"id":"0cb41b6d-29da-52fc-814a-b657fd90b1e7","excerpt":"Why Module is important?ReusabilityComposabilityLeverageIsolation (Easy to maintain)OrganizationCommonJSCommonJs is one of the standards which NodeJS has been using. Before ES6, browsers didn’t support module system. Basically Syntaxs like CommonJS and AMD made it possible for…","html":"<h2 id=\"why-module-is-important\"><a href=\"#why-module-is-important\" aria-label=\"why module is important permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why Module is important?</h2>\n<ul>\n<li>Reusability</li>\n<li>Composability</li>\n<li>Leverage</li>\n<li>Isolation (Easy to maintain)</li>\n<li>Organization</li>\n</ul>\n<h2 id=\"commonjs\"><a href=\"#commonjs\" aria-label=\"commonjs permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>CommonJS</h2>\n<p>CommonJs is one of the standards which <strong>NodeJS</strong> has been using. Before ES6, browsers didn’t support module system. Basically Syntaxs like CommonJS and AMD made it possible for browsers and server engines to use module system.</p>\n<p>This is how the module system works in CommonJS.</p>\n<div class=\"gatsby-highlight has-highlighted-lines\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// greeting.js</span>\n\n<span class=\"token keyword\">let</span> <span class=\"token function-variable function\">hello</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'hello'</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"gatsby-highlight-code-line\">exports<span class=\"token punctuation\">.</span>greeting <span class=\"token operator\">=</span> hello</span>\n<span class=\"token comment\">// example.js</span>\n\n<span class=\"gatsby-highlight-code-line\"><span class=\"token keyword\">let</span> greeting <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'./greeting.js'</span><span class=\"token punctuation\">)</span></span>\ngreeting<span class=\"token punctuation\">.</span><span class=\"token function\">hello</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// 'hello'</span></code></pre></div>\n<h2 id=\"babel--webpack\"><a href=\"#babel--webpack\" aria-label=\"babel  webpack permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Babel &#x26; Webpack</h2>\n<p>Though ES Modules now landed in the browsers, there are some old versions of browsers or servers that don’t understand the syntax of ES Modules. To make our codes compatible for those environments, transpiling process (put simply, <strong>translating es6 syntax to es5</strong>) is necessary.</p>\n<p>The well-known ones are <strong>babel</strong> and bundling tool, <strong>webpack</strong>.\nA JavaScript bundler gathers the separated codes into one .js file and you can set it up for es5 conversion as well if there are codes written in es6.</p>\n<h2 id=\"conclusion\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>Transpilers are still needed for a smooth unification of client-side code and on server-side(NodeJS) code.</p>\n<br>\n<br>\n<p><em>Reference</em></p>\n<p><a href=\"https://flaviocopes.com/es-modules/\">https://flaviocopes.com/es-modules/</a></p>\n<br>\n<br>","frontmatter":{"title":"JavaScript Concept 12 - Modules in JavaScript (CommonJS, Babel & Webpack)","date":"July 16, 2019"}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"slug":"/JavaScript/javascript-concept-12---commonjs/","previous":{"fields":{"slug":"/JavaScript/javascript-concept-11---callback-promise-async&await/"},"frontmatter":{"title":"JavaScript Concept 11 - Callback, Promise and Async/Await","category":"JavaScript"}},"next":{"fields":{"slug":"/development/rest-api/"},"frontmatter":{"title":"The difference between API and REST API","category":"Development"}}}}}