{"id":272,"date":"2026-10-30T13:06:11","date_gmt":"2026-10-30T13:06:11","guid":{"rendered":"https:\/\/devdojo.co.in\/?p=272"},"modified":"2026-10-30T13:06:12","modified_gmt":"2026-10-30T13:06:12","slug":"nodejs-series-part-2-modules-esm-commonjs","status":"publish","type":"post","link":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/","title":{"rendered":"Node.js Part 2: Modules in Node.js: ES Modules, CommonJS and import\/export"},"content":{"rendered":"\n<div class=\"dd-post\">\n<style>\n.single article:has(.dd-hero) .entry-media{display:none}\n.dd-post{--navy:#0b1020;--blue:#3178c6;--violet:#7c5cff;--ink:#1f2433;--muted:#5b6275;--line:#e3e6ef;font-size:17px;line-height:1.75;color:var(--ink)}\n.dd-post h2{margin:2.2em 0 .6em;font-size:1.6em;line-height:1.3;scroll-margin-top:90px}\n.dd-post h3{margin:1.6em 0 .5em;font-size:1.22em;scroll-margin-top:90px}\n.dd-post a{color:var(--blue);text-decoration:underline;text-underline-offset:3px}\n.dd-post pre{background:#0f1430;color:#e6e9f5;padding:18px 20px;border-radius:12px;overflow-x:auto;line-height:1.6;border:1px solid #232a52;margin:1.2em 0;font-size:14.5px !important}\n.dd-post pre code{font-size:1em !important;background:none;color:inherit;padding:0;font-family:\"JetBrains Mono\",\"Fira Code\",Consolas,Menlo,monospace;white-space:pre}\n.dd-post :not(pre)>code{background:#eef1fb;color:#2b3a8f;padding:2px 6px;border-radius:5px;font-size:.9em}\n.dd-post .dd-file{display:inline-block;background:#151b3b;color:#c9d1ff;font:600 13px\/1 \"JetBrains Mono\",Consolas,monospace;padding:7px 12px;border-radius:8px 8px 0 0;margin:1em 0 -1.2em;position:relative}\n.dd-post .dd-hero{margin:0 0 1.6em;border-radius:16px;overflow:hidden;box-shadow:0 10px 30px rgba(11,16,32,.25)}\n.dd-post .dd-hero svg{display:block;width:100%;height:auto}\n.dd-post .dd-hero .node{transition:transform .25s,filter .25s;transform-box:fill-box;transform-origin:center;cursor:pointer}\n.dd-post .dd-hero .node:hover{transform:scale(1.08);filter:drop-shadow(0 0 10px #7c5cff)}\n.dd-post .dd-series{display:inline-flex;align-items:center;gap:8px;background:#151b3b;color:#dfe3ff;border-radius:999px;padding:6px 14px;font-size:.88em;font-weight:600;margin:0 0 .8em}\n.dd-post .dd-series span{background:var(--violet);color:#fff;border-radius:999px;padding:2px 10px}\n.dd-post .dd-prereq{font-size:.96em;color:var(--muted);border-left:3px solid var(--line);padding-left:12px;margin:.6em 0 1.2em}\n.dd-post .dd-summary{background:linear-gradient(135deg,#eef3ff,#f4efff);border:1px solid #d6dcf5;border-left:5px solid var(--violet);border-radius:12px;padding:18px 22px;margin:1.4em 0}\n.dd-post .dd-summary strong.lbl{display:block;font-size:1.05em;margin-bottom:6px;color:#3a2fa0}\n.dd-post .dd-summary ul{margin:.3em 0 0 1.1em}\n.dd-post .dd-toc{background:#f8f9fc;border:1px solid var(--line);border-radius:12px;padding:16px 22px;margin:1.4em 0}\n.dd-post .dd-toc ol{margin:.4em 0 0 1.2em;columns:2;column-gap:28px}\n.dd-post .dd-toc li{break-inside:avoid;margin:.15em 0}\n@media(max-width:640px){.dd-post .dd-toc ol{columns:1}}\n.dd-post .dd-tip,.dd-post .dd-note,.dd-post .dd-warn{border-radius:10px;padding:14px 18px;margin:1.3em 0}\n.dd-post .dd-tip{background:#ecfbf3;border-left:4px solid #1f9d61}\n.dd-post .dd-note{background:#eef4ff;border-left:4px solid var(--blue)}\n.dd-post .dd-warn{background:#fff6e8;border-left:4px solid #e08a00}\n.dd-post .dd-cards{display:grid;grid-template-columns:repeat(auto-fit,minmax(210px,1fr));gap:14px;margin:1.3em 0}\n.dd-post .dd-card{border:1px solid var(--line);border-radius:12px;padding:14px 16px;background:#fff;box-shadow:0 2px 8px rgba(20,30,70,.05)}\n.dd-post .dd-card b{display:block;color:#3a2fa0;margin-bottom:4px}\n.dd-post table{width:100%;border-collapse:collapse;margin:1.3em 0;font-size:.95em}\n.dd-post th,.dd-post td{border:1px solid var(--line);padding:10px 12px;text-align:left;vertical-align:top}\n.dd-post th{background:#151b3b;color:#fff}\n.dd-post tr:nth-child(even) td{background:#f8f9fc}\n.dd-post .dd-tablewrap{overflow-x:auto}\n.dd-post figure{margin:1.6em 0;text-align:center}\n.dd-post figure svg{width:100%;height:auto;border-radius:12px}\n.dd-post figcaption{font-size:.9em;color:var(--muted);margin-top:8px}\n.dd-post details{border:1px solid var(--line);border-radius:10px;padding:12px 16px;margin:.7em 0;background:#fff}\n.dd-post details summary{cursor:pointer;font-weight:600}\n.dd-post details[open] summary{margin-bottom:8px;color:#3a2fa0}\n.dd-post .dd-roadmap{border:2px solid #d6dcf5;background:#fbfbff}\n.dd-post .dd-roadmap h4{margin:1em 0 .3em;color:#151b3b;font-size:1em;text-transform:uppercase;letter-spacing:.04em}\n.dd-post .dd-roadmap ol{margin:.2em 0 .6em 1.4em;padding:0}\n.dd-post .dd-roadmap li{margin:.2em 0}\n.dd-post .dd-roadmap .here{background:var(--violet);color:#fff;border-radius:999px;padding:1px 9px;font-size:.8em;font-weight:700;margin-left:6px}\n.dd-post .dd-roadmap .soon{color:#8a90a6;font-size:.82em;margin-left:6px}\n.dd-post .dd-tabs{border:1px solid var(--line);border-radius:12px;padding:6px 16px 16px;margin:1.4em 0;background:#fbfbfe}\n.dd-post .dd-tabs>input{position:absolute;opacity:0;pointer-events:none}\n.dd-post .dd-tabs .labels label{display:inline-block;padding:8px 16px;margin:10px 6px 0 0;border-radius:999px;border:1px solid #cfd5ea;cursor:pointer;font-weight:600;font-size:.95em;background:#fff;transition:background .2s}\n.dd-post .dd-tabs .labels label:hover{border-color:var(--violet)}\n.dd-post .dd-tabs .panel{display:none;margin-top:12px}\n.dd-post .dd-tabs #ti1:checked~.labels label[for=ti1],.dd-post .dd-tabs #ti2:checked~.labels label[for=ti2],.dd-post .dd-tabs #ti3:checked~.labels label[for=ti3],.dd-post .dd-tabs #ti4:checked~.labels label[for=ti4]{background:var(--violet);color:#fff;border-color:var(--violet)}\n.dd-post .dd-tabs #ti1:checked~.p1,.dd-post .dd-tabs #ti2:checked~.p2,.dd-post .dd-tabs #ti3:checked~.p3,.dd-post .dd-tabs #ti4:checked~.p4{display:block}\n.dd-post .dd-tabs #ti1:focus-visible~.labels label[for=ti1],.dd-post .dd-tabs #ti2:focus-visible~.labels label[for=ti2],.dd-post .dd-tabs #ti3:focus-visible~.labels label[for=ti3],.dd-post .dd-tabs #ti4:focus-visible~.labels label[for=ti4]{outline:2px solid var(--blue);outline-offset:2px}\n.dd-post .dd-cta{background:linear-gradient(135deg,#0b1020,#151b3b);color:#e8ebff;border-radius:16px;padding:24px 26px;margin:2em 0;text-align:center}\n.dd-post .dd-cta h3{color:#fff;margin-top:0}\n.dd-post .dd-cta a{color:#a99bff}\n.dd-post .dd-vsc{position:relative;margin:1.5em 0;border-radius:12px;overflow:hidden;background:#1e1e1e;border:1px solid #2b2b2b;box-shadow:0 14px 34px rgba(0,0,0,.28);font-family:\"Segoe UI\",system-ui,sans-serif}\n.dd-post .dd-vsc>input{position:absolute;opacity:0;pointer-events:none}\n.dd-post .dd-vsc-bar{display:flex;align-items:center;gap:12px;background:#323233;color:#cccccc;font-size:12.5px;padding:8px 12px}\n.dd-post .dd-dots{display:inline-flex;gap:7px}.dd-post .dd-dots i{width:12px;height:12px;border-radius:50%;background:#ff5f57;display:block}.dd-post .dd-dots i:nth-child(2){background:#febc2e}.dd-post .dd-dots i:nth-child(3){background:#28c840}\n.dd-post .dd-vsc-title{flex:1;text-align:center;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;padding-right:50px;opacity:.85}\n.dd-post .dd-vsc-tabs{display:flex;align-items:stretch;background:#252526;border-bottom:1px solid #1e1e1e;overflow-x:auto}\n.dd-post .dd-vsc-tabs label{display:flex;align-items:center;gap:7px;padding:9px 16px;color:#969696;font-size:13px;cursor:pointer;background:#2d2d2d;border-right:1px solid #252526;border-top:1px solid transparent;white-space:nowrap;margin:0;transition:color .15s}\n.dd-post .dd-vsc-tabs label:hover{color:#e0e0e0}\n.dd-post .dd-vsc-tabs .ic{font:700 9.5px\/1 Consolas,monospace;padding:3px 4px;border-radius:3px;color:#1e1e1e}\n.dd-post .ic.py{background:#ffd43b}.dd-post .ic.js{background:#f1e05a}.dd-post .ic.sh{background:#89e051}\n.dd-post .dd-vsc-tabs label.run{margin-left:auto;background:transparent;border:0;color:#89d185;font-weight:600;display:none}\n.dd-post .dd-vsc-tabs label.run:hover{color:#b5f0b0;background:#2a2d2e}\n.dd-post .dd-vsc-pane{display:none}\n.dd-post .dd-vsc>.f1:checked~.dd-vsc-tabs .t1,.dd-post .dd-vsc>.f2:checked~.dd-vsc-tabs .t2,.dd-post .dd-vsc>.f3:checked~.dd-vsc-tabs .t3{background:#1e1e1e;color:#ffffff;border-top-color:#3178c6}\n.dd-post .dd-vsc>.f1:checked~.p1,.dd-post .dd-vsc>.f2:checked~.p2,.dd-post .dd-vsc>.f3:checked~.p3{display:block}\n.dd-post .dd-vsc>.f1:checked~.dd-vsc-tabs .run1,.dd-post .dd-vsc>.f2:checked~.dd-vsc-tabs .run2,.dd-post .dd-vsc>.f3:checked~.dd-vsc-tabs .run3{display:flex}\n.dd-post .dd-vsc>.f1:focus-visible~.dd-vsc-tabs .t1,.dd-post .dd-vsc>.f2:focus-visible~.dd-vsc-tabs .t2,.dd-post .dd-vsc>.f3:focus-visible~.dd-vsc-tabs .t3,.dd-post .dd-vsc>input[type=checkbox]:focus-visible~.dd-vsc-tabs .run{outline:2px solid #3178c6;outline-offset:-2px}\n.dd-post .dd-vsc-crumbs{background:#1e1e1e;color:#a9a9a9;font-size:12px;padding:5px 16px 2px}\n.dd-post .dd-vsc pre.dd-vsc-code{background:#1e1e1e;color:#d4d4d4;border:0;border-radius:0;margin:0;padding:8px 0 14px;counter-reset:ln;line-height:1.62;overflow-x:auto}\n.dd-post .dd-vsc pre code{font-family:Consolas,\"JetBrains Mono\",\"Fira Code\",Menlo,monospace;color:inherit;background:none;white-space:pre}\n.dd-post .dd-vsc .l{display:inline-block;min-width:100%;padding-right:18px;box-sizing:border-box}\n.dd-post .dd-vsc .l::before{counter-increment:ln;content:counter(ln);display:inline-block;width:3.2em;padding-right:1.4em;text-align:right;color:#6e7681;user-select:none;-webkit-user-select:none}\n.dd-post .dd-vsc .l:hover{background:#2a2d2e}.dd-post .dd-vsc .l:hover::before{color:#c6c6c6}\n.dd-post .dd-vsc .k,.dd-post .dd-vsc .kn{color:#c586c0}.dd-post .dd-vsc .kc{color:#569cd6}.dd-post .dd-vsc .s{color:#ce9178}.dd-post .dd-vsc .c{color:#6a9955}.dd-post .dd-vsc .m{color:#b5cea8}.dd-post .dd-vsc .nf{color:#dcdcaa}.dd-post .dd-vsc .nc{color:#4ec9b0}.dd-post .dd-vsc .nb{color:#4ec9b0}.dd-post .dd-vsc .nd{color:#dcdcaa}.dd-post .dd-vsc .n{color:#9cdcfe}.dd-post .dd-vsc .o{color:#d4d4d4}.dd-post .dd-vsc .gp{color:#89d185;user-select:none;-webkit-user-select:none}\n.dd-post .dd-vsc-term{display:none;border-top:1px solid #3c3c3c;background:#181818}\n.dd-post .dd-vsc>.r1:checked~.p1 .dd-vsc-term,.dd-post .dd-vsc>.r2:checked~.p2 .dd-vsc-term,.dd-post .dd-vsc>.r3:checked~.p3 .dd-vsc-term{display:block;animation:ddterm .35s ease-out}\n@keyframes ddterm{from{opacity:0;transform:translateY(6px)}to{opacity:1;transform:none}}\n.dd-post .dd-term-head{display:flex;gap:18px;align-items:center;font-size:11px;letter-spacing:.06em;color:#8b8b8b;padding:7px 16px 0}\n.dd-post .dd-term-head span:first-child{color:#e7e7e7;border-bottom:1px solid #e7e7e7;padding-bottom:4px}\n.dd-post .dd-term-head .dd-term-note{margin-left:auto;letter-spacing:0;text-transform:none;color:#d7ba7d;border:0;font-size:11.5px}\n.dd-post .dd-vsc-term pre{background:#181818;color:#cccccc;border:0;margin:0;padding:10px 16px 14px;border-radius:0}\n.dd-post .dd-vsc-status{display:flex;flex-wrap:wrap;gap:4px 14px;background:#007acc;color:#fff;font-size:11.5px;padding:4px 12px}\n.dd-post .dd-vsc-status .sp{flex:1}\n.dd-post .dd-vsc-termonly pre.dd-vsc-code{background:#181818;padding:12px 16px}\n@media(max-width:640px){.dd-post .dd-vsc-title{padding-right:0;text-align:left}.dd-post .dd-vsc-status span:nth-child(n+4){display:none}.dd-post .dd-vsc .l::before{width:2.2em;padding-right:1em}}\n.dd-post .dd-vsc-hint{font-size:.93em;color:var(--muted);margin-top:-.6em}\n.dd-post .dd-next{border:1px dashed var(--violet);border-radius:12px;padding:16px 20px;margin:1.6em 0;background:#f7f4ff}\n.dd-post .ic.yml{background:#cb3837;color:#fff}\n.dd-post .ic.jsx{background:#61dafb}.dd-post .ic.html{background:#e44d26;color:#fff}.dd-post .ic.css{background:#563d7c;color:#fff}\n.dd-post .dd-steps{counter-reset:st;list-style:none;margin:1.2em 0;padding:0}\n.dd-post .dd-steps>li{position:relative;padding:0 0 0 46px;margin:0 0 14px}\n.dd-post .dd-steps>li::before{counter-increment:st;content:counter(st);position:absolute;left:0;top:2px;width:32px;height:32px;border-radius:50%;background:linear-gradient(135deg,#3178c6,#7c5cff);color:#fff;font-weight:700;text-align:center;line-height:32px}\n<\/style>\n\n<div class=\"dd-hero\">\n<svg viewBox=\"0 0 1200 520\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" role=\"img\" aria-label=\"Node.js series Part 2 banner: a CommonJS box with require and an ES Modules box with import, both feeding small module files into one Node.js app\">\n<defs>\n<linearGradient id=\"ddbg\" x1=\"0\" y1=\"0\" x2=\"1\" y2=\"1\"><stop offset=\"0\" stop-color=\"#0b1020\"\/><stop offset=\"1\" stop-color=\"#151b3b\"\/><\/linearGradient>\n<linearGradient id=\"ddac\" x1=\"0\" y1=\"0\" x2=\"1\" y2=\"0\"><stop offset=\"0\" stop-color=\"#3178c6\"\/><stop offset=\"1\" stop-color=\"#7c5cff\"\/><\/linearGradient>\n<linearGradient id=\"ddgr\" x1=\"0\" y1=\"0\" x2=\"1\" y2=\"1\"><stop offset=\"0\" stop-color=\"#5fa04e\"\/><stop offset=\"1\" stop-color=\"#3c873a\"\/><\/linearGradient>\n<radialGradient id=\"ddglow\" cx=\".5\" cy=\".5\" r=\".5\"><stop offset=\"0\" stop-color=\"#7c5cff\" stop-opacity=\".45\"\/><stop offset=\"1\" stop-color=\"#7c5cff\" stop-opacity=\"0\"\/><\/radialGradient>\n<\/defs>\n<rect x=\"0\" y=\"-60\" width=\"1200\" height=\"640\" fill=\"url(#ddbg)\"\/>\n<circle cx=\"930\" cy=\"250\" r=\"270\" fill=\"url(#ddglow)\"\/>\n<g opacity=\".12\" stroke=\"#9fb2ff\"><path d=\"M0 100H1200M0 200H1200M0 300H1200M0 400H1200M200 0V520M400 0V520M600 0V520M800 0V520M1000 0V520\"\/><\/g>\n<rect x=\"60\" y=\"46\" width=\"128\" height=\"38\" rx=\"19\" fill=\"url(#ddac)\"\/>\n<text x=\"124\" y=\"71\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"18\" font-weight=\"700\" fill=\"#fff\">PART 2<\/text>\n<text x=\"204\" y=\"71\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"18\" fill=\"#aab4e8\">Node.js Series \u00b7 DevDojo<\/text>\n<text x=\"60\" y=\"148\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"52\" font-weight=\"800\" fill=\"#ffffff\">Node.js Modules<\/text>\n<text x=\"60\" y=\"206\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"52\" font-weight=\"800\" fill=\"#ffffff\">ES Modules vs CommonJS<\/text>\n<text x=\"60\" y=\"250\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"24\" fill=\"#c9d1ff\">import, export, require and module.exports made simple<\/text>\n<g class=\"node\"><rect x=\"60\" y=\"300\" width=\"210\" height=\"64\" rx=\"14\" fill=\"#0b1020\" stroke=\"#f1e05a\" stroke-width=\"2.5\"\/><text x=\"165\" y=\"328\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"19\" font-weight=\"800\" fill=\"#f1e05a\">CommonJS<\/text><text x=\"165\" y=\"352\" text-anchor=\"middle\" font-family=\"Consolas,monospace\" font-size=\"15\" fill=\"#c9d1ff\">require()<\/text><\/g>\n<g class=\"node\"><rect x=\"60\" y=\"390\" width=\"210\" height=\"64\" rx=\"14\" fill=\"#0b1020\" stroke=\"#7c5cff\" stroke-width=\"2.5\"\/><text x=\"165\" y=\"418\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"19\" font-weight=\"800\" fill=\"#a99bff\">ES Modules<\/text><text x=\"165\" y=\"442\" text-anchor=\"middle\" font-family=\"Consolas,monospace\" font-size=\"15\" fill=\"#c9d1ff\">import \/ export<\/text><\/g>\n<path d=\"M270 332H340V376H400M270 422H340V376\" stroke=\"#7c5cff\" stroke-width=\"2.5\" fill=\"none\" opacity=\".8\"\/>\n<g class=\"node\"><rect x=\"400\" y=\"344\" width=\"200\" height=\"64\" rx=\"32\" fill=\"url(#ddac)\"\/><text x=\"500\" y=\"383\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"20\" font-weight=\"800\" fill=\"#fff\">app.js<\/text><\/g>\n<path id=\"ddflow\" d=\"M270 332H340V376H400\" fill=\"none\" stroke=\"none\"\/>\n<circle r=\"6\" fill=\"#f1e05a\"><animateMotion dur=\"2.6s\" repeatCount=\"indefinite\"><mpath href=\"#ddflow\"\/><\/animateMotion><\/circle>\n<path id=\"ddflow2\" d=\"M270 422H340V376H400\" fill=\"none\" stroke=\"none\"\/>\n<circle r=\"6\" fill=\"#61dafb\"><animateMotion dur=\"2.6s\" begin=\"1.3s\" repeatCount=\"indefinite\"><mpath href=\"#ddflow2\"\/><\/animateMotion><\/circle>\n<g transform=\"translate(930 250)\">\n<path id=\"ddo1\" d=\"M-210 0 A210 82 0 1 1 210 0 A210 82 0 1 1 -210 0 Z\" fill=\"none\" stroke=\"url(#ddac)\" stroke-width=\"3\" opacity=\".7\"\/>\n<g class=\"node\"><polygon points=\"0,-80 69,-40 69,40 0,80 -69,40 -69,-40\" fill=\"url(#ddgr)\" stroke=\"#8fd16a\" stroke-width=\"3\"\/><text y=\"12\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"30\" font-weight=\"800\" fill=\"#fff\">JS<\/text><\/g>\n<g class=\"node\"><rect x=\"-260\" y=\"-128\" width=\"104\" height=\"40\" rx=\"10\" fill=\"#151b3b\" stroke=\"#3178c6\" stroke-width=\"2\"\/><text x=\"-208\" y=\"-102\" text-anchor=\"middle\" font-family=\"Consolas,monospace\" font-size=\"16\" fill=\"#c9d1ff\">math.js<\/text><\/g>\n<g class=\"node\"><rect x=\"160\" y=\"-128\" width=\"104\" height=\"40\" rx=\"10\" fill=\"#151b3b\" stroke=\"#3178c6\" stroke-width=\"2\"\/><text x=\"212\" y=\"-102\" text-anchor=\"middle\" font-family=\"Consolas,monospace\" font-size=\"16\" fill=\"#c9d1ff\">db.js<\/text><\/g>\n<g class=\"node\"><rect x=\"-60\" y=\"112\" width=\"120\" height=\"40\" rx=\"10\" fill=\"#151b3b\" stroke=\"#3178c6\" stroke-width=\"2\"\/><text x=\"0\" y=\"138\" text-anchor=\"middle\" font-family=\"Consolas,monospace\" font-size=\"16\" fill=\"#c9d1ff\">utils.cjs<\/text><\/g>\n<circle r=\"9\" fill=\"#ffffff\"><animateMotion dur=\"5s\" repeatCount=\"indefinite\"><mpath href=\"#ddo1\"\/><\/animateMotion><\/circle>\n<circle r=\"8\" fill=\"#7c5cff\"><animateMotion dur=\"5s\" begin=\"-2.5s\" repeatCount=\"indefinite\"><mpath href=\"#ddo1\"\/><\/animateMotion><\/circle>\n<\/g>\n<text x=\"660\" y=\"470\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"17\" fill=\"#aab4e8\">Small files, clear imports, one app<\/text>\n<text x=\"60\" y=\"505\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"16\" fill=\"#7f89b5\">devdojo.co.in \u00b7 Beginner friendly \u00b7 Works on Node.js 22, 24 and 26<\/text>\n<\/svg>\n<\/div>\n\n<div class=\"dd-series\">Series: Node.js <span>Part 2 of 30<\/span><\/div>\n<p class=\"dd-prereq\"><strong>What you should know first:<\/strong> read <a href=\"https:\/\/devdojo.co.in\/index.php\/2026\/10\/23\/nodejs-series-part-1-what-is-nodejs\/\">Node.js Part 1: What Is Node.js and How to Build Your First Node.js App<\/a>. You should have Node.js installed and know how to run a file with <code>node file.js<\/code>.<\/p>\n\n<p>In Part 1 you wrote everything in one file. That is fine for ten lines, but real apps have hundreds of files: routes, database code, helpers, configuration. <strong>Node.js modules<\/strong> are how you split code into small files and share functions between them. In this part you will learn, in simple words, the two module systems Node.js supports: the modern <strong>ES Modules<\/strong> (<code>import<\/code> \/ <code>export<\/code>) and the older <strong>CommonJS<\/strong> (<code>require<\/code> \/ <code>module.exports<\/code>). You will see how Node.js decides which one a file uses, how to mix them safely, and how to fix the errors every beginner meets.<\/p>\n\n<div class=\"dd-summary\">\n<strong class=\"lbl\">Quick Summary<\/strong>\n<ul>\n<li>A <strong>module<\/strong> is simply one JavaScript file. Anything you <strong>export<\/strong> from it can be used by other files; everything else stays private.<\/li>\n<li><strong>ES Modules (ESM)<\/strong> use <code>import<\/code> and <code>export<\/code>. They are the JavaScript standard and the best choice for new projects.<\/li>\n<li><strong>CommonJS (CJS)<\/strong> uses <code>require()<\/code> and <code>module.exports<\/code>. It is older but still used by many npm packages.<\/li>\n<li>Node.js picks the system by <strong>file extension<\/strong>: <code>.mjs<\/code> = ESM, <code>.cjs<\/code> = CommonJS, and <code>.js<\/code> follows the <code>\"type\"<\/code> field in <code>package.json<\/code>.<\/li>\n<li>ESM can import CommonJS files, and modern Node.js (22.12+) can also <code>require()<\/code> ES modules. Modules run once and are then cached.<\/li>\n<\/ul>\n<\/div>\n\n<details class=\"dd-roadmap\">\n<summary>Full Node.js roadmap (30 parts)<\/summary>\n<h4>Beginner<\/h4>\n<ol start=\"1\">\n<li><a href=\"https:\/\/devdojo.co.in\/index.php\/2026\/10\/23\/nodejs-series-part-1-what-is-nodejs\/\">What Is Node.js and How to Build Your First Node.js App<\/a><\/li>\n<li>Modules in Node.js: ES Modules, CommonJS and import\/export <span class=\"here\">You are here<\/span><\/li>\n<li>npm and package.json: Installing and Managing Packages <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Working with Files and Folders: the fs and path Modules <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Asynchronous Node.js: Callbacks, Promises and async\/await <span class=\"soon\">Coming soon<\/span><\/li>\n<li>The Event Loop Explained Simply <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Events and EventEmitter: Building Event-Driven Code <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Streams and Buffers: Handling Large Data Efficiently <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Building a Web Server with the http Module <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Mini Project: Build a Command-Line Tool with Node.js <span class=\"soon\">Coming soon<\/span><\/li>\n<\/ol>\n<h4>Intermediate<\/h4>\n<ol start=\"11\">\n<li>Express.js Basics: Routes, Requests and Responses <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Middleware in Express: How Requests Flow <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Build a REST API with Express: Full CRUD <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Validating Request Data with Zod <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Environment Variables and Configuration (&#8211;env-file and .env) <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Connecting Node.js to PostgreSQL <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Using an ORM: Prisma vs Drizzle <span class=\"soon\">Coming soon<\/span><\/li>\n<li>MongoDB with Mongoose <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Authentication: Password Hashing, Sessions and JWT <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Error Handling and Logging with Pino <span class=\"soon\">Coming soon<\/span><\/li>\n<\/ol>\n<h4>Advanced<\/h4>\n<ol start=\"21\">\n<li>Testing Node.js Apps with node:test and Supertest <span class=\"soon\">Coming soon<\/span><\/li>\n<li>TypeScript with Node.js: Running .ts Files Directly <span class=\"soon\">Coming soon<\/span><\/li>\n<li>File Uploads and Image Processing <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Real-Time Apps with WebSockets and Socket.IO <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Caching with Redis <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Background Jobs and Queues with BullMQ <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Worker Threads and Clustering for Performance <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Security Best Practices for Node.js APIs <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Debugging, Profiling and Performance Tuning <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Final Project: Build and Deploy a Production-Ready Node.js API <span class=\"soon\">Coming soon<\/span><\/li>\n<\/ol>\n<\/details>\n\n<nav class=\"dd-toc\" aria-label=\"Table of contents\">\n<strong>In this post<\/strong>\n<ol>\n<li><a href=\"#what-is-a-module\">What is a module?<\/a><\/li>\n<li><a href=\"#two-systems\">ES Modules vs CommonJS<\/a><\/li>\n<li><a href=\"#commonjs\">CommonJS: require and module.exports<\/a><\/li>\n<li><a href=\"#es-modules\">ES Modules: import and export<\/a><\/li>\n<li><a href=\"#how-node-decides\">How Node.js decides the module type<\/a><\/li>\n<li><a href=\"#mixing\">Mixing ESM and CommonJS<\/a><\/li>\n<li><a href=\"#esm-extras\">__dirname, JSON and dynamic import()<\/a><\/li>\n<li><a href=\"#cache\">Modules run once: the module cache<\/a><\/li>\n<li><a href=\"#errors\">Common errors and fixes<\/a><\/li>\n<li><a href=\"#best-practices\">Best practices<\/a><\/li>\n<li><a href=\"#practice\">Practice task<\/a><\/li>\n<li><a href=\"#faq\">FAQ<\/a><\/li>\n<\/ol>\n<\/nav>\n<\/div>\n\n\n\n<div class=\"dd-post\">\n<h2 id=\"what-is-a-module\">What is a module in Node.js?<\/h2>\n<p>A <strong>module<\/strong> is just a JavaScript file that keeps its own variables to itself. Think of each file as a small shop with a front counter. Inside the shop there are many things (variables, helper functions), but customers only get what is put on the counter. In code, \u201cputting something on the counter\u201d is called <strong>exporting<\/strong>, and \u201cbuying it from another file\u201d is called <strong>importing<\/strong>.<\/p>\n<p>Modules give you three big benefits:<\/p>\n<div class=\"dd-cards\">\n<div class=\"dd-card\"><b>Organisation<\/b>Each file does one job: <code>db.js<\/code> talks to the database, <code>mail.js<\/code> sends email. Easy to find, easy to change.<\/div>\n<div class=\"dd-card\"><b>Privacy<\/b>Variables are private to their file by default. Two files can both have a variable called <code>count<\/code> without any clash.<\/div>\n<div class=\"dd-card\"><b>Reuse<\/b>Write a helper once and import it everywhere. npm packages are just modules written by other people.<\/div>\n<\/div>\n<p>Node.js has three kinds of modules you will import: <strong>your own files<\/strong> (like <code>.\/math.js<\/code>), <strong>built-in modules<\/strong> that come with Node.js (like <code>node:fs<\/code>, <code>node:path<\/code>, <code>node:os<\/code>), and <strong>npm packages<\/strong> you install (like <code>express<\/code>; we cover npm in Part 3).<\/p>\n\n<h2 id=\"two-systems\">ES Modules vs CommonJS: two ways to share code<\/h2>\n<p>When Node.js was created in 2009, JavaScript had no built-in module system, so Node.js invented its own: <strong>CommonJS<\/strong>. Years later, JavaScript itself got an official standard called <strong>ES Modules<\/strong> (ESM), which browsers also understand. Today Node.js supports both, which is why you see two different styles in tutorials and packages.<\/p>\n<div class=\"dd-tablewrap\">\n<table>\n<thead><tr><th>Feature<\/th><th>ES Modules (modern)<\/th><th>CommonJS (older)<\/th><\/tr><\/thead>\n<tbody>\n<tr><td>Share code<\/td><td><code>export<\/code> \/ <code>export default<\/code><\/td><td><code>module.exports<\/code> \/ <code>exports<\/code><\/td><\/tr>\n<tr><td>Use code<\/td><td><code>import \u2026 from \"\u2026\"<\/code><\/td><td><code>const x = require(\"\u2026\")<\/code><\/td><\/tr>\n<tr><td>File extension<\/td><td><code>.mjs<\/code>, or <code>.js<\/code> with <code>\"type\": \"module\"<\/code><\/td><td><code>.cjs<\/code>, or <code>.js<\/code> without <code>\"type\": \"module\"<\/code><\/td><\/tr>\n<tr><td>Loading<\/td><td>Imports are found before the code runs<\/td><td><code>require()<\/code> runs line by line, when reached<\/td><\/tr>\n<tr><td>Top-level <code>await<\/code><\/td><td>Yes<\/td><td>No<\/td><\/tr>\n<tr><td><code>__dirname<\/code>, <code>__filename<\/code><\/td><td>Use <code>import.meta.dirname<\/code> \/ <code>import.meta.filename<\/code><\/td><td>Available<\/td><\/tr>\n<tr><td>Works in browsers<\/td><td>Yes<\/td><td>No<\/td><\/tr>\n<tr><td>Best for<\/td><td>All new projects<\/td><td>Older projects and older packages<\/td><\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div class=\"dd-tip\"><strong>Simple rule:<\/strong> start every new project with ES Modules. Learn CommonJS so you can read older code and Stack Overflow answers, and so you know how to fix errors when the two meet.<\/div>\n\n<h2 id=\"commonjs\">CommonJS: require and module.exports<\/h2>\n<p>Let&#8217;s start with the older style, because you will see it in many existing projects. In CommonJS, each file has a special object called <code>module.exports<\/code>. Whatever you put on it is what other files receive when they call <code>require()<\/code>. We use the <code>.cjs<\/code> extension so Node.js always treats these files as CommonJS.<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: math.cjs, app.cjs\">\n<input type=\"radio\" name=\"v1\" id=\"v1f1\" class=\"f1\" checked>\n<input type=\"radio\" name=\"v1\" id=\"v1f2\" class=\"f2\">\n<input type=\"checkbox\" id=\"v1r2\" class=\"r2\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">math.cjs \u2014 node-part2 \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v1f1\" class=\"t1\"><b class=\"ic js\">JS<\/b>math.cjs<\/label>\n<label for=\"v1f2\" class=\"t2\"><b class=\"ic js\">JS<\/b>app.cjs<\/label>\n<label for=\"v1r2\" class=\"run run2\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">node-part2 \u203a math.cjs<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ math.cjs \u2013 a CommonJS module<\/span><\/span>\n<span class=\"l\"><span class=\"k\">const<\/span> <span class=\"n\">PI<\/span> = <span class=\"m\">3.14159<\/span>;<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">function<\/span> <span class=\"nf\">add<\/span>(<span class=\"n\">a<\/span>, <span class=\"n\">b<\/span>) {<\/span>\n<span class=\"l\">  <span class=\"k\">return<\/span> <span class=\"n\">a<\/span> + <span class=\"n\">b<\/span>;<\/span>\n<span class=\"l\">}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">function<\/span> <span class=\"nf\">circleArea<\/span>(<span class=\"n\">r<\/span>) {<\/span>\n<span class=\"l\">  <span class=\"k\">return<\/span> <span class=\"n\">PI<\/span> * <span class=\"n\">r<\/span> * <span class=\"n\">r<\/span>;<\/span>\n<span class=\"l\">}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\">\/\/ Choose what other files can use<\/span><\/span>\n<span class=\"l\"><span class=\"nb\">module<\/span>.<span class=\"n\">exports<\/span> = { <span class=\"n\">add<\/span>, <span class=\"n\">circleArea<\/span> };<\/span><\/code><\/pre>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 13, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<div class=\"dd-vsc-pane p2\">\n<div class=\"dd-vsc-crumbs\">node-part2 \u203a app.cjs<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ app.cjs \u2013 load our module with require()<\/span><\/span>\n<span class=\"l\"><span class=\"k\">const<\/span> { <span class=\"n\">add<\/span>, <span class=\"n\">circleArea<\/span> } = <span class=\"nb\">require<\/span>(<span class=\"s\">\".\/math.cjs\"<\/span>);<\/span>\n<span class=\"l\"><span class=\"k\">const<\/span> <span class=\"n\">path<\/span> = <span class=\"nb\">require<\/span>(<span class=\"s\">\"node:path\"<\/span>);<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"2 + 3 =\"<\/span>, <span class=\"nf\">add<\/span>(<span class=\"m\">2<\/span>, <span class=\"m\">3<\/span>));<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"Area of r=2:\"<\/span>, <span class=\"nf\">circleArea<\/span>(<span class=\"m\">2<\/span>).<span class=\"nf\">toFixed<\/span>(<span class=\"m\">2<\/span>));<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"This file:\"<\/span>, <span class=\"n\">path<\/span>.<span class=\"nf\">basename<\/span>(<span class=\"n\">__filename<\/span>));<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"Type of require:\"<\/span>, <span class=\"k\">typeof<\/span> <span class=\"nb\">require<\/span>);<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Real output (Node.js 22, Linux)<\/span><\/div><pre><code><span class=\"gp\">$<\/span> node app.cjs\n2 + 3 = 5\nArea of r=2: 12.57\nThis file: app.cjs\nType of require: function<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 8, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Click the <strong>app.cjs<\/strong> tab, then <strong>\u25b6 Run<\/strong> to see the real output.<\/p>\n<p>What is happening here:<\/p>\n<ul>\n<li><code>PI<\/code> is <strong>not<\/strong> exported, so it stays private to <code>math.cjs<\/code>. Only <code>add<\/code> and <code>circleArea<\/code> are shared.<\/li>\n<li><code>require(\".\/math.cjs\")<\/code> runs the file and returns its <code>module.exports<\/code> object. We use <strong>destructuring<\/strong> (<code>{ add, circleArea }<\/code>) to take just the two functions.<\/li>\n<li>The <code>.\/<\/code> at the start means \u201ca file next to this one\u201d. Without it, Node.js would look for a built-in module or an npm package called <code>math.cjs<\/code>.<\/li>\n<li><code>require(\"node:path\")<\/code> loads a built-in module. The <code>node:<\/code> prefix makes that clear.<\/li>\n<li><code>__filename<\/code> and <code>require<\/code> are not real globals. Node.js quietly wraps every CommonJS file in a function and passes them in. That is why they do not exist in ES modules.<\/li>\n<\/ul>\n<div class=\"dd-warn\"><strong>Watch out for <code>exports = \u2026<\/code>:<\/strong> CommonJS also gives you a shortcut variable called <code>exports<\/code>. You can write <code>exports.add = add<\/code>, but never <code>exports = { add }<\/code>. Replacing the variable breaks its link to <code>module.exports<\/code>, so the other file receives an empty object. When in doubt, always use <code>module.exports<\/code>.<\/div>\n\n<h2 id=\"es-modules\">ES Modules: import and export<\/h2>\n<p>Now the modern way. First tell Node.js that the <code>.js<\/code> files in this project are ES modules by adding <code>\"type\": \"module\"<\/code> to <code>package.json<\/code>:<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: package.json, math.js, app.js\">\n<input type=\"radio\" name=\"v2\" id=\"v2f1\" class=\"f1\" checked>\n<input type=\"radio\" name=\"v2\" id=\"v2f2\" class=\"f2\">\n<input type=\"radio\" name=\"v2\" id=\"v2f3\" class=\"f3\">\n<input type=\"checkbox\" id=\"v2r3\" class=\"r3\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">package.json \u2014 node-part2 \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v2f1\" class=\"t1\"><b class=\"ic yml\">{}<\/b>package.json<\/label>\n<label for=\"v2f2\" class=\"t2\"><b class=\"ic js\">JS<\/b>math.js<\/label>\n<label for=\"v2f3\" class=\"t3\"><b class=\"ic js\">JS<\/b>app.js<\/label>\n<label for=\"v2r3\" class=\"run run3\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">node-part2 \u203a package.json<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\">{<\/span>\n<span class=\"l\">  <span class=\"n\">\"name\"<\/span>: <span class=\"s\">\"node-part2\"<\/span>,<\/span>\n<span class=\"l\">  <span class=\"n\">\"version\"<\/span>: <span class=\"s\">\"1.0.0\"<\/span>,<\/span>\n<span class=\"l\">  <span class=\"n\">\"type\"<\/span>: <span class=\"s\">\"module\"<\/span><\/span>\n<span class=\"l\">}<\/span><\/code><\/pre>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 5, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JSON<\/span><\/div>\n<\/div>\n<div class=\"dd-vsc-pane p2\">\n<div class=\"dd-vsc-crumbs\">node-part2 \u203a math.js<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ math.js \u2013 an ES module (package.json has \"type\": \"module\")<\/span><\/span>\n<span class=\"l\"><span class=\"k\">export<\/span> <span class=\"k\">const<\/span> <span class=\"n\">PI<\/span> = <span class=\"m\">3.14159<\/span>;<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">export<\/span> <span class=\"k\">function<\/span> <span class=\"nf\">add<\/span>(<span class=\"n\">a<\/span>, <span class=\"n\">b<\/span>) {<\/span>\n<span class=\"l\">  <span class=\"k\">return<\/span> <span class=\"n\">a<\/span> + <span class=\"n\">b<\/span>;<\/span>\n<span class=\"l\">}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">export<\/span> <span class=\"k\">function<\/span> <span class=\"nf\">circleArea<\/span>(<span class=\"n\">r<\/span>) {<\/span>\n<span class=\"l\">  <span class=\"k\">return<\/span> <span class=\"n\">PI<\/span> * <span class=\"n\">r<\/span> * <span class=\"n\">r<\/span>;<\/span>\n<span class=\"l\">}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\">\/\/ One default export per file (optional)<\/span><\/span>\n<span class=\"l\"><span class=\"k\">export<\/span> <span class=\"k\">default<\/span> <span class=\"k\">function<\/span> <span class=\"nf\">greet<\/span>(<span class=\"n\">name<\/span>) {<\/span>\n<span class=\"l\">  <span class=\"k\">return<\/span> <span class=\"s\">`Hello, ${<\/span><span class=\"n\">name<\/span><span class=\"s\">}!`<\/span>;<\/span>\n<span class=\"l\">}<\/span><\/code><\/pre>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 15, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<div class=\"dd-vsc-pane p3\">\n<div class=\"dd-vsc-crumbs\">node-part2 \u203a app.js<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ app.js \u2013 import named and default exports<\/span><\/span>\n<span class=\"l\"><span class=\"k\">import<\/span> <span class=\"n\">greet<\/span>, { <span class=\"n\">add<\/span>, <span class=\"n\">circleArea<\/span> <span class=\"k\">as<\/span> <span class=\"n\">area<\/span> } <span class=\"k\">from<\/span> <span class=\"s\">\".\/math.js\"<\/span>;<\/span>\n<span class=\"l\"><span class=\"k\">import<\/span> * <span class=\"k\">as<\/span> <span class=\"n\">math<\/span> <span class=\"k\">from<\/span> <span class=\"s\">\".\/math.js\"<\/span>;<\/span>\n<span class=\"l\"><span class=\"k\">import<\/span> { <span class=\"n\">readFile<\/span> } <span class=\"k\">from<\/span> <span class=\"s\">\"node:fs\/promises\"<\/span>;<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"nf\">greet<\/span>(<span class=\"s\">\"DevDojo\"<\/span>));<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"2 + 3 =\"<\/span>, <span class=\"nf\">add<\/span>(<span class=\"m\">2<\/span>, <span class=\"m\">3<\/span>));<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"Area of r=2:\"<\/span>, <span class=\"nf\">area<\/span>(<span class=\"m\">2<\/span>).<span class=\"nf\">toFixed<\/span>(<span class=\"m\">2<\/span>));<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"All exports:\"<\/span>, <span class=\"nb\">Object<\/span>.<span class=\"nf\">keys<\/span>(<span class=\"n\">math<\/span>));<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">const<\/span> <span class=\"n\">pkg<\/span> = <span class=\"nb\">JSON<\/span>.<span class=\"nf\">parse<\/span>(<span class=\"k\">await<\/span> <span class=\"nf\">readFile<\/span>(<span class=\"s\">\".\/package.json\"<\/span>, <span class=\"s\">\"utf8\"<\/span>));<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"Package type:\"<\/span>, <span class=\"n\">pkg<\/span>.<span class=\"n\">type<\/span>);<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Real output (Node.js 22, Linux)<\/span><\/div><pre><code><span class=\"gp\">$<\/span> node app.js\nHello, DevDojo!\n2 + 3 = 5\nArea of r=2: 12.57\nAll exports: [ 'PI', 'add', 'circleArea', 'default' ]\nPackage type: module<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 12, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Click the three tabs to read each file, then <strong>\u25b6 Run<\/strong> on <strong>app.js<\/strong>.<\/p>\n<p>There are two kinds of exports in ES modules:<\/p>\n<ul>\n<li><strong>Named exports<\/strong> (<code>export const PI<\/code>, <code>export function add<\/code>): a file can have many. You import them with curly braces and the <strong>exact same name<\/strong>: <code>import { add } from \".\/math.js\"<\/code>. You can rename on import with <code>as<\/code>: <code>{ circleArea as area }<\/code>.<\/li>\n<li><strong>Default export<\/strong> (<code>export default<\/code>): at most one per file. You import it <strong>without<\/strong> curly braces and can give it any name: <code>import greet from \".\/math.js\"<\/code>.<\/li>\n<li><code>import * as math<\/code> collects every export into one object. The output shows a key called <code>default<\/code>: that is where the default export lives.<\/li>\n<\/ul>\n<p>Notice two more things in <code>app.js<\/code>: the file path includes the extension (<code>.\/math.js<\/code>, not <code>.\/math<\/code>), and we used <code>await<\/code> at the top level of the file, outside any <code>async<\/code> function. Both are normal in ES modules.<\/p>\n<div class=\"dd-note\"><strong>Named or default?<\/strong> Many teams prefer <strong>named exports<\/strong> for most code. Names must match on import, so typos are caught early and editors can auto-import and rename them safely. Use a default export when a file clearly has one main thing, like one class or one config object.<\/div>\n\n<h2 id=\"how-node-decides\">How Node.js decides: ESM or CommonJS?<\/h2>\n<p>This is the part that confuses most beginners, so let&#8217;s make it simple. Node.js looks at the <strong>file extension first<\/strong>, and only for plain <code>.js<\/code> files does it check <code>package.json<\/code>. Click the tabs below to see each case.<\/p>\n<div class=\"dd-tabs\">\n<input type=\"radio\" name=\"ddt\" id=\"ti1\" checked><input type=\"radio\" name=\"ddt\" id=\"ti2\"><input type=\"radio\" name=\"ddt\" id=\"ti3\"><input type=\"radio\" name=\"ddt\" id=\"ti4\">\n<div class=\"labels\"><label for=\"ti1\">.mjs file<\/label><label for=\"ti2\">.cjs file<\/label><label for=\"ti3\">.js + &#8220;type&#8221;: &#8220;module&#8221;<\/label><label for=\"ti4\">.js without &#8220;type&#8221;<\/label><\/div>\n<div class=\"panel p1\"><p><strong>Always an ES module.<\/strong> The <code>.mjs<\/code> extension means \u201cmodule JavaScript\u201d. Use <code>import<\/code>\/<code>export<\/code>. It does not matter what <code>package.json<\/code> says.<\/p><\/div>\n<div class=\"panel p2\"><p><strong>Always CommonJS.<\/strong> The <code>.cjs<\/code> extension means \u201cCommonJS JavaScript\u201d. Use <code>require<\/code>\/<code>module.exports<\/code>. Handy for one old-style config file inside a modern ESM project.<\/p><\/div>\n<div class=\"panel p3\"><p><strong>ES module.<\/strong> The nearest <code>package.json<\/code> (searching upwards through the folders) has <code>\"type\": \"module\"<\/code>, so every <code>.js<\/code> file in that project is ESM. This is the setup we recommend for new projects.<\/p><\/div>\n<div class=\"panel p4\"><p><strong>CommonJS by default<\/strong>, for backwards compatibility. But modern Node.js (22.7 and newer) is smart: if the file contains ESM-only syntax like <code>import<\/code> or <code>export<\/code>, it re-runs it as an ES module. This \u201cdetection\u201d is a helpful fallback, not a plan. It costs a little start-up time, so always set <code>\"type\"<\/code> explicitly.<\/p><\/div>\n<\/div>\n<figure>\n<svg viewBox=\"0 0 900 360\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" role=\"img\" aria-label=\"Flowchart: a .mjs file is an ES module, a .cjs file is CommonJS, and a .js file follows the type field in package.json\">\n<rect width=\"900\" height=\"360\" fill=\"#0b1020\"\/>\n<g font-family=\"Segoe UI,Arial,sans-serif\" text-anchor=\"middle\">\n<rect x=\"360\" y=\"20\" width=\"180\" height=\"46\" rx=\"23\" fill=\"url(#ddac)\"\/><text x=\"450\" y=\"49\" font-size=\"18\" font-weight=\"700\" fill=\"#fff\">Your file<\/text>\n<path d=\"M450 66V96M150 96H750M150 96V122M450 96V122M750 96V122\" stroke=\"#7c5cff\" stroke-width=\"2.5\" fill=\"none\"\/>\n<rect x=\"70\" y=\"122\" width=\"160\" height=\"44\" rx=\"10\" fill=\"#151b3b\" stroke=\"#3178c6\" stroke-width=\"2\"\/><text x=\"150\" y=\"150\" font-size=\"17\" fill=\"#c9d1ff\" font-family=\"Consolas,monospace\">.mjs<\/text>\n<rect x=\"370\" y=\"122\" width=\"160\" height=\"44\" rx=\"10\" fill=\"#151b3b\" stroke=\"#3178c6\" stroke-width=\"2\"\/><text x=\"450\" y=\"150\" font-size=\"17\" fill=\"#c9d1ff\" font-family=\"Consolas,monospace\">.js<\/text>\n<rect x=\"670\" y=\"122\" width=\"160\" height=\"44\" rx=\"10\" fill=\"#151b3b\" stroke=\"#3178c6\" stroke-width=\"2\"\/><text x=\"750\" y=\"150\" font-size=\"17\" fill=\"#c9d1ff\" font-family=\"Consolas,monospace\">.cjs<\/text>\n<path d=\"M150 166V280M750 166V280M450 166V196\" stroke=\"#7c5cff\" stroke-width=\"2.5\" fill=\"none\"\/>\n<rect x=\"320\" y=\"196\" width=\"260\" height=\"50\" rx=\"10\" fill=\"#151b3b\" stroke=\"#f1e05a\" stroke-width=\"2\"\/><text x=\"450\" y=\"218\" font-size=\"15\" fill=\"#f1e05a\">package.json has<\/text><text x=\"450\" y=\"238\" font-size=\"15\" fill=\"#f1e05a\" font-family=\"Consolas,monospace\">&#8220;type&#8221;: &#8220;module&#8221;?<\/text>\n<path d=\"M380 246V262H220V280M520 246V262H680V280\" stroke=\"#7c5cff\" stroke-width=\"2.5\" fill=\"none\"\/>\n<text x=\"290\" y=\"256\" font-size=\"14\" fill=\"#8fd16a\">yes<\/text><text x=\"610\" y=\"256\" font-size=\"14\" fill=\"#ff8a8a\">no<\/text>\n<rect x=\"60\" y=\"280\" width=\"250\" height=\"56\" rx=\"12\" fill=\"#2a1f66\" stroke=\"#7c5cff\" stroke-width=\"2\"\/><text x=\"185\" y=\"305\" font-size=\"18\" font-weight=\"700\" fill=\"#fff\">ES Module<\/text><text x=\"185\" y=\"325\" font-size=\"13\" fill=\"#c9d1ff\">import \/ export<\/text>\n<rect x=\"590\" y=\"280\" width=\"250\" height=\"56\" rx=\"12\" fill=\"#3a3410\" stroke=\"#f1e05a\" stroke-width=\"2\"\/><text x=\"715\" y=\"305\" font-size=\"18\" font-weight=\"700\" fill=\"#fff\">CommonJS<\/text><text x=\"715\" y=\"325\" font-size=\"13\" fill=\"#e8e1a8\">require \/ module.exports<\/text>\n<\/g>\n<circle r=\"6\" fill=\"#61dafb\"><animateMotion dur=\"3s\" repeatCount=\"indefinite\" path=\"M450 66V96H450V196V262H220V280\"\/><\/circle>\n<\/svg>\n<figcaption>Node.js checks the file extension first. Only plain <code>.js<\/code> files look at the <code>\"type\"<\/code> field in the nearest <code>package.json<\/code>.<\/figcaption>\n<\/figure>\n<p>You can create the <code>package.json<\/code> from the terminal instead of by hand:<\/p>\n<div class=\"dd-vsc dd-vsc-termonly\" role=\"group\" aria-label=\"Terminal: Terminal \u2014 node-part2\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">Terminal \u2014 node-part2<\/span><\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># create package.json and switch the project to ES modules<\/span><\/span>\n<span class=\"l\"><span class=\"gp\">$<\/span> npm init -y<\/span>\n<span class=\"l\"><span class=\"gp\">$<\/span> npm pkg set type=module<\/span>\n<span class=\"l\"><span class=\"gp\">$<\/span> npm pkg get type<\/span>\n<span class=\"l\">\"module\"<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<div class=\"dd-post\">\n<h2 id=\"mixing\">Mixing ES Modules and CommonJS<\/h2>\n<p>In real projects you will often use an old CommonJS package from a modern ESM app, or the other way round. Good news: both directions work in current Node.js versions.<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: use-cjs.mjs, use-esm.cjs, logger.cjs\">\n<input type=\"radio\" name=\"v3\" id=\"v3f1\" class=\"f1\" checked>\n<input type=\"radio\" name=\"v3\" id=\"v3f2\" class=\"f2\">\n<input type=\"radio\" name=\"v3\" id=\"v3f3\" class=\"f3\">\n<input type=\"checkbox\" id=\"v3r1\" class=\"r1\">\n<input type=\"checkbox\" id=\"v3r2\" class=\"r2\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">use-cjs.mjs \u2014 mixing-modules \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v3f1\" class=\"t1\"><b class=\"ic js\">JS<\/b>use-cjs.mjs<\/label>\n<label for=\"v3f2\" class=\"t2\"><b class=\"ic js\">JS<\/b>use-esm.cjs<\/label>\n<label for=\"v3f3\" class=\"t3\"><b class=\"ic js\">JS<\/b>logger.cjs<\/label>\n<label for=\"v3r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<label for=\"v3r2\" class=\"run run2\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">mixing-modules \u203a use-cjs.mjs<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ use-cjs.mjs \u2013 ESM can import CommonJS<\/span><\/span>\n<span class=\"l\"><span class=\"k\">import<\/span> <span class=\"n\">logger<\/span> <span class=\"k\">from<\/span> <span class=\"s\">\".\/logger.cjs\"<\/span>;      <span class=\"c\">\/\/ module.exports object<\/span><\/span>\n<span class=\"l\"><span class=\"k\">import<\/span> { <span class=\"n\">log<\/span> } <span class=\"k\">from<\/span> <span class=\"s\">\".\/logger.cjs\"<\/span>;     <span class=\"c\">\/\/ named import also works<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nf\">log<\/span>(<span class=\"s\">\"ESM imported a CommonJS file\"<\/span>);<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"level:\"<\/span>, <span class=\"n\">logger<\/span>.<span class=\"n\">level<\/span>);<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Real output (Node.js 22, Linux)<\/span><\/div><pre><code><span class=\"gp\">$<\/span> node use-cjs.mjs\n[log] ESM imported a CommonJS file\nlevel: info<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 6, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<div class=\"dd-vsc-pane p2\">\n<div class=\"dd-vsc-crumbs\">mixing-modules \u203a use-esm.cjs<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ use-esm.cjs \u2013 CommonJS can require() ES modules (Node.js 22.12+)<\/span><\/span>\n<span class=\"l\"><span class=\"k\">const<\/span> <span class=\"n\">config<\/span> = <span class=\"nb\">require<\/span>(<span class=\"s\">\".\/config.mjs\"<\/span>);<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"port:\"<\/span>, <span class=\"n\">config<\/span>.<span class=\"n\">port<\/span>);<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"default:\"<\/span>, <span class=\"n\">config<\/span>.<span class=\"k\">default<\/span>.<span class=\"n\">appName<\/span>);<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Real output (Node.js 22, Linux)<\/span><\/div><pre><code><span class=\"gp\">$<\/span> node use-esm.cjs\nport: 3000\ndefault: DevDojo API<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 5, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<div class=\"dd-vsc-pane p3\">\n<div class=\"dd-vsc-crumbs\">mixing-modules \u203a logger.cjs<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ logger.cjs \u2013 an old CommonJS helper<\/span><\/span>\n<span class=\"l\"><span class=\"k\">function<\/span> <span class=\"nf\">log<\/span>(<span class=\"n\">msg<\/span>) {<\/span>\n<span class=\"l\">  <span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">`[log] ${<\/span><span class=\"n\">msg<\/span><span class=\"s\">}`<\/span>);<\/span>\n<span class=\"l\">}<\/span>\n<span class=\"l\"><span class=\"nb\">module<\/span>.<span class=\"n\">exports<\/span> = { <span class=\"n\">log<\/span>, <span class=\"n\">level<\/span>: <span class=\"s\">\"info\"<\/span> };<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\">\/\/ config.mjs \u2013 a modern ES module<\/span><\/span>\n<span class=\"l\"><span class=\"k\">export<\/span> <span class=\"k\">const<\/span> <span class=\"n\">port<\/span> = <span class=\"m\">3000<\/span>;<\/span>\n<span class=\"l\"><span class=\"k\">export<\/span> <span class=\"k\">default<\/span> { <span class=\"n\">appName<\/span>: <span class=\"s\">\"DevDojo API\"<\/span> };<\/span><\/code><\/pre>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 9, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Two directions: click each tab and <strong>\u25b6 Run<\/strong>. The third tab shows the two helper files (<code>logger.cjs<\/code> and <code>config.mjs<\/code>).<\/p>\n<ul>\n<li><strong>ESM importing CommonJS<\/strong> has always worked. The <code>module.exports<\/code> object becomes the <strong>default import<\/strong>. Node.js also tries to detect named exports, so <code>import { log } from \".\/logger.cjs\"<\/code> usually works too.<\/li>\n<li><strong>CommonJS requiring ESM<\/strong> (<code>require()<\/code> of an ES module) is the newer feature. It is enabled by default from Node.js 22.12 and 20.19, and you get the module&#8217;s namespace object: named exports as properties and the default export under <code>.default<\/code>.<\/li>\n<li>One limit: <code>require()<\/code> cannot load an ES module that uses <strong>top-level <code>await<\/code><\/strong>. Node.js throws <code>ERR_REQUIRE_ASYNC_MODULE<\/code>. In that case use <code>await import(\".\/file.mjs\")<\/code> inside an async function.<\/li>\n<\/ul>\n<div class=\"dd-tip\"><strong>Tip:<\/strong> mixing is great for moving an old project to ESM step by step. Rename files one at a time, run your app after each change, and keep old helpers as <code>.cjs<\/code> until you have time to convert them.<\/div>\n\n<h2 id=\"esm-extras\">ESM extras: __dirname, JSON files and dynamic import()<\/h2>\n<p>Three questions come up as soon as people switch to ES modules: \u201cWhere did <code>__dirname<\/code> go?\u201d, \u201cHow do I load a JSON file?\u201d and \u201cCan I load a module only when needed?\u201d. One small file answers all three:<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: paths.js, settings.json\">\n<input type=\"radio\" name=\"v4\" id=\"v4f1\" class=\"f1\" checked>\n<input type=\"radio\" name=\"v4\" id=\"v4f2\" class=\"f2\">\n<input type=\"checkbox\" id=\"v4r1\" class=\"r1\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">paths.js \u2014 esm-extras \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v4f1\" class=\"t1\"><b class=\"ic js\">JS<\/b>paths.js<\/label>\n<label for=\"v4f2\" class=\"t2\"><b class=\"ic yml\">{}<\/b>settings.json<\/label>\n<label for=\"v4r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">esm-extras \u203a paths.js<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ paths.js \u2013 ESM replacements for __dirname, JSON and lazy loading<\/span><\/span>\n<span class=\"l\"><span class=\"k\">import<\/span> <span class=\"n\">path<\/span> <span class=\"k\">from<\/span> <span class=\"s\">\"node:path\"<\/span>;<\/span>\n<span class=\"l\"><span class=\"k\">import<\/span> <span class=\"n\">settings<\/span> <span class=\"k\">from<\/span> <span class=\"s\">\".\/settings.json\"<\/span> <span class=\"k\">with<\/span> { <span class=\"n\">type<\/span>: <span class=\"s\">\"json\"<\/span> };<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"Folder:\"<\/span>, <span class=\"n\">path<\/span>.<span class=\"nf\">basename<\/span>(<span class=\"k\">import<\/span>.<span class=\"n\">meta<\/span>.<span class=\"n\">dirname<\/span>));<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"File:\"<\/span>, <span class=\"n\">path<\/span>.<span class=\"nf\">basename<\/span>(<span class=\"k\">import<\/span>.<span class=\"n\">meta<\/span>.<span class=\"n\">filename<\/span>));<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"Settings:\"<\/span>, <span class=\"n\">settings<\/span>);<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\">\/\/ Dynamic import(): load a module only when you need it<\/span><\/span>\n<span class=\"l\"><span class=\"k\">const<\/span> <span class=\"n\">useColors<\/span> = <span class=\"nb\">process<\/span>.<span class=\"n\">argv<\/span>.<span class=\"nf\">includes<\/span>(<span class=\"s\">\"--color\"<\/span>);<\/span>\n<span class=\"l\"><span class=\"k\">if<\/span> (<span class=\"n\">useColors<\/span>) {<\/span>\n<span class=\"l\">  <span class=\"k\">const<\/span> { <span class=\"n\">styleText<\/span> } = <span class=\"k\">await<\/span> <span class=\"k\">import<\/span>(<span class=\"s\">\"node:util\"<\/span>);<\/span>\n<span class=\"l\">  <span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"nf\">styleText<\/span>(<span class=\"s\">\"green\"<\/span>, <span class=\"s\">\"Colors loaded on demand\"<\/span>));<\/span>\n<span class=\"l\">} <span class=\"k\">else<\/span> {<\/span>\n<span class=\"l\">  <span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"Colors module was not loaded\"<\/span>);<\/span>\n<span class=\"l\">}<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Real output (Node.js 22, Linux)<\/span><\/div><pre><code><span class=\"gp\">$<\/span> node paths.js\nFolder: esm-extras\nFile: paths.js\nSettings: { theme: 'dark', retries: 3 }\nColors module was not loaded\n$ node paths.js --color\nColors loaded on demand<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 16, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<div class=\"dd-vsc-pane p2\">\n<div class=\"dd-vsc-crumbs\">esm-extras \u203a settings.json<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\">{ <span class=\"n\">\"theme\"<\/span>: <span class=\"s\">\"dark\"<\/span>, <span class=\"n\">\"retries\"<\/span>: <span class=\"m\">3<\/span> }<\/span><\/code><\/pre>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 1, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JSON<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Click <strong>\u25b6 Run<\/strong>: the second command loads <code>node:util<\/code> only because <code>--color<\/code> was passed.<\/p>\n<ul>\n<li><code>import.meta.dirname<\/code> and <code>import.meta.filename<\/code> are the ESM versions of <code>__dirname<\/code> and <code>__filename<\/code>. Use them to build paths to files next to your code, like <code>path.join(import.meta.dirname, \"data.txt\")<\/code>.<\/li>\n<li><code>with { type: \"json\" }<\/code> is an <strong>import attribute<\/strong>. It tells Node.js \u201cthis file must be JSON\u201d, which is a safety check so a JSON import can never run code by surprise.<\/li>\n<li><code>await import(\"node:util\")<\/code> is a <strong>dynamic import<\/strong>. It works like a function, returns a promise, and loads the module only when that line runs. It is useful for big, rarely used features, plugins, or loading ESM from CommonJS. (<code>styleText<\/code> from <code>node:util<\/code> colours terminal text without any npm package.)<\/li>\n<\/ul>\n\n<h2 id=\"cache\">Modules run once: the module cache<\/h2>\n<p>An important fact that surprises many developers: <strong>a module&#8217;s code runs only the first time it is imported<\/strong>. After that, Node.js keeps it in a cache, and every later import receives the same, already-loaded module. This is why a database connection created in <code>db.js<\/code> is shared by your whole app instead of being opened again for every file.<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: main.js, counter.js, a.js\">\n<input type=\"radio\" name=\"v5\" id=\"v5f1\" class=\"f1\" checked>\n<input type=\"radio\" name=\"v5\" id=\"v5f2\" class=\"f2\">\n<input type=\"radio\" name=\"v5\" id=\"v5f3\" class=\"f3\">\n<input type=\"checkbox\" id=\"v5r1\" class=\"r1\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">main.js \u2014 module-cache \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v5f1\" class=\"t1\"><b class=\"ic js\">JS<\/b>main.js<\/label>\n<label for=\"v5f2\" class=\"t2\"><b class=\"ic js\">JS<\/b>counter.js<\/label>\n<label for=\"v5f3\" class=\"t3\"><b class=\"ic js\">JS<\/b>a.js<\/label>\n<label for=\"v5r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">module-cache \u203a main.js<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ main.js \u2013 modules are cached and shared<\/span><\/span>\n<span class=\"l\"><span class=\"k\">import<\/span> <span class=\"s\">\".\/a.js\"<\/span>;<\/span>\n<span class=\"l\"><span class=\"k\">import<\/span> { <span class=\"n\">count<\/span>, <span class=\"n\">increment<\/span> } <span class=\"k\">from<\/span> <span class=\"s\">\".\/counter.js\"<\/span>;<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nf\">increment<\/span>();<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"count is\"<\/span>, <span class=\"n\">count<\/span>); <span class=\"c\">\/\/ live binding: sees both increments<\/span><\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Real output (Node.js 22, Linux)<\/span><\/div><pre><code><span class=\"gp\">$<\/span> node main.js\ncounter.js is loading...\ncount is 2<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 6, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<div class=\"dd-vsc-pane p2\">\n<div class=\"dd-vsc-crumbs\">module-cache \u203a counter.js<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ counter.js \u2013 runs only ONCE, no matter how many imports<\/span><\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"nf\">log<\/span>(<span class=\"s\">\"counter.js is loading...\"<\/span>);<\/span>\n<span class=\"l\"><span class=\"k\">export<\/span> <span class=\"k\">let<\/span> <span class=\"n\">count<\/span> = <span class=\"m\">0<\/span>;<\/span>\n<span class=\"l\"><span class=\"k\">export<\/span> <span class=\"k\">function<\/span> <span class=\"nf\">increment<\/span>() {<\/span>\n<span class=\"l\">  <span class=\"n\">count<\/span>++;<\/span>\n<span class=\"l\">}<\/span><\/code><\/pre>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 6, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<div class=\"dd-vsc-pane p3\">\n<div class=\"dd-vsc-crumbs\">module-cache \u203a a.js<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"k\">import<\/span> { <span class=\"n\">increment<\/span> } <span class=\"k\">from<\/span> <span class=\"s\">\".\/counter.js\"<\/span>;<\/span>\n<span class=\"l\"><span class=\"nf\">increment<\/span>();<\/span><\/code><\/pre>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 2, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Both <code>main.js<\/code> and <code>a.js<\/code> import <code>counter.js<\/code>. Click <strong>\u25b6 Run<\/strong> and count how many times \u201cloading\u201d prints.<\/p>\n<figure>\n<svg viewBox=\"0 0 900 300\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" role=\"img\" aria-label=\"Diagram: main.js and a.js both import counter.js, which is loaded once into the module cache and shared\">\n<rect width=\"900\" height=\"300\" fill=\"#0b1020\"\/>\n<g font-family=\"Segoe UI,Arial,sans-serif\" text-anchor=\"middle\">\n<rect x=\"60\" y=\"50\" width=\"180\" height=\"56\" rx=\"12\" fill=\"#151b3b\" stroke=\"#3178c6\" stroke-width=\"2\"\/><text x=\"150\" y=\"84\" font-size=\"18\" fill=\"#c9d1ff\" font-family=\"Consolas,monospace\">main.js<\/text>\n<rect x=\"60\" y=\"190\" width=\"180\" height=\"56\" rx=\"12\" fill=\"#151b3b\" stroke=\"#3178c6\" stroke-width=\"2\"\/><text x=\"150\" y=\"224\" font-size=\"18\" fill=\"#c9d1ff\" font-family=\"Consolas,monospace\">a.js<\/text>\n<rect x=\"340\" y=\"90\" width=\"230\" height=\"116\" rx=\"16\" fill=\"#151b3b\" stroke=\"#7c5cff\" stroke-width=\"2\" stroke-dasharray=\"6 5\"\/><text x=\"455\" y=\"116\" font-size=\"14\" fill=\"#aab4e8\">MODULE CACHE<\/text>\n<rect x=\"370\" y=\"130\" width=\"170\" height=\"56\" rx=\"12\" fill=\"url(#ddac)\"\/><text x=\"455\" y=\"164\" font-size=\"18\" font-weight=\"700\" fill=\"#fff\" font-family=\"Consolas,monospace\">counter.js<\/text>\n<path d=\"M240 78H300V150H370M240 218H300V166H370\" stroke=\"#7c5cff\" stroke-width=\"2.5\" fill=\"none\"\/>\n<rect x=\"640\" y=\"104\" width=\"220\" height=\"88\" rx=\"12\" fill=\"#10251a\" stroke=\"#1f9d61\" stroke-width=\"2\"\/><text x=\"750\" y=\"136\" font-size=\"16\" fill=\"#8fd16a\">Runs once<\/text><text x=\"750\" y=\"162\" font-size=\"16\" fill=\"#c9d1ff\">count = 2 everywhere<\/text>\n<path d=\"M540 158H640\" stroke=\"#1f9d61\" stroke-width=\"2.5\"\/>\n<\/g>\n<circle r=\"6\" fill=\"#61dafb\"><animateMotion dur=\"2.4s\" repeatCount=\"indefinite\" path=\"M240 78H300V150H370\"\/><\/circle>\n<circle r=\"6\" fill=\"#f1e05a\"><animateMotion dur=\"2.4s\" begin=\"1.2s\" repeatCount=\"indefinite\" path=\"M240 218H300V166H370\"\/><\/circle>\n<\/svg>\n<figcaption>Both files import <code>counter.js<\/code>, but its code runs once. Everyone shares the same cached copy.<\/figcaption>\n<\/figure>\n<p>ES modules also give you <strong>live bindings<\/strong>: the imported <code>count<\/code> is not a copy, it is a live view of the variable inside <code>counter.js<\/code>. That is why <code>main.js<\/code> sees <code>2<\/code> after both increments. (You cannot assign to an imported variable directly, though: <code>count = 5<\/code> in <code>main.js<\/code> throws an error. Change it through an exported function instead.)<\/p>\n<\/div>\n\n\n\n<div class=\"dd-post\">\n<h2 id=\"errors\">Common errors and fixes<\/h2>\n<p>Here is the error you will almost certainly see on your first day with ES modules: using <code>require<\/code> inside an ESM file.<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: oops.js\">\n<input type=\"radio\" name=\"v6\" id=\"v6f1\" class=\"f1\" checked>\n<input type=\"checkbox\" id=\"v6r1\" class=\"r1\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">oops.js \u2014 esm-extras \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v6f1\" class=\"t1\"><b class=\"ic js\">JS<\/b>oops.js<\/label>\n<label for=\"v6r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">esm-extras \u203a oops.js<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"k\">const<\/span> <span class=\"n\">fs<\/span> = <span class=\"nb\">require<\/span>(<span class=\"s\">\"node:fs\"<\/span>);<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Real error (Node.js 22, path shortened)<\/span><\/div><pre><code><span class=\"gp\">$<\/span> node oops.js\nfile:\/\/\/home\/dev\/esm-extras\/oops.js:1\nconst fs = require(\"node:fs\");\n           ^\n\nReferenceError: require is not defined in ES module scope, you can use import instead\nThis file is being treated as an ES module because it has a '.js' file extension and '\/home\/dev\/esm-extras\/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 1, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Click <strong>\u25b6 Run<\/strong> to see the real error. Node.js even tells you the fix: use <code>import<\/code> or rename the file to <code>.cjs<\/code>.<\/p>\n<div class=\"dd-tablewrap\">\n<table>\n<thead><tr><th>Error message<\/th><th>Why it happens<\/th><th>How to fix it<\/th><\/tr><\/thead>\n<tbody>\n<tr><td><code>ReferenceError: require is not defined in ES module scope<\/code><\/td><td>The file is an ES module (<code>.mjs<\/code> or <code>\"type\": \"module\"<\/code>) but uses <code>require<\/code>.<\/td><td>Use <code>import<\/code>, or rename the file to <code>.cjs<\/code>.<\/td><\/tr>\n<tr><td><code>SyntaxError: Cannot use import statement outside a module<\/code><\/td><td>The file is treated as CommonJS (often a <code>.cjs<\/code> file, or a tool that does not detect ESM).<\/td><td>Add <code>\"type\": \"module\"<\/code> to <code>package.json<\/code> or rename the file to <code>.mjs<\/code>.<\/td><\/tr>\n<tr><td><code>Error [ERR_MODULE_NOT_FOUND]: Cannot find module '\u2026\/math'<\/code><\/td><td>ES modules need the full file name; <code>.\/math<\/code> is not enough.<\/td><td>Write <code>.\/math.js<\/code> with the extension.<\/td><\/tr>\n<tr><td><code>ReferenceError: __dirname is not defined in ES module scope<\/code><\/td><td><code>__dirname<\/code> only exists in CommonJS.<\/td><td>Use <code>import.meta.dirname<\/code> (Node.js 20.11+).<\/td><\/tr>\n<tr><td><code>SyntaxError: The requested module '.\/x.js' does not provide an export named 'y'<\/code><\/td><td>Name typo, or the file uses a default export.<\/td><td>Check the spelling, or import without braces: <code>import y from \".\/x.js\"<\/code>.<\/td><\/tr>\n<tr><td>Required object is empty <code>{}<\/code><\/td><td><code>exports = {\u2026}<\/code> was used in a CommonJS file.<\/td><td>Use <code>module.exports = {\u2026}<\/code>.<\/td><\/tr>\n<tr><td><code>ERR_REQUIRE_ASYNC_MODULE<\/code><\/td><td><code>require()<\/code> of an ES module that uses top-level <code>await<\/code>.<\/td><td>Use <code>await import()<\/code> instead.<\/td><\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n\n<h2 id=\"best-practices\">Best practices for Node.js modules<\/h2>\n<ol class=\"dd-steps\">\n<li><strong>Use ES Modules for new code.<\/strong> Add <code>\"type\": \"module\"<\/code> to <code>package.json<\/code> on day one. It matches the browser, TypeScript and modern tools.<\/li>\n<li><strong>Always write file extensions<\/strong> in relative imports (<code>.\/utils.js<\/code>). It is required in ESM and makes code clearer.<\/li>\n<li><strong>Use the <code>node:<\/code> prefix<\/strong> for built-in modules (<code>node:fs<\/code>, <code>node:path<\/code>). It can never clash with an npm package of the same name.<\/li>\n<li><strong>Prefer named exports<\/strong> for helpers. Keep one clear job per file, and keep files small.<\/li>\n<li><strong>Avoid circular imports<\/strong> (a.js imports b.js and b.js imports a.js). They can give you <code>undefined<\/code> or \u201ccannot access before initialization\u201d errors. Move shared code into a third file.<\/li>\n<li><strong>Do not rely on automatic detection.<\/strong> Set <code>\"type\"<\/code> explicitly or use <code>.mjs<\/code>\/<code>.cjs<\/code>, so every tool reads your files the same way.<\/li>\n<\/ol>\n<p>For the full details, the official <a href=\"https:\/\/nodejs.org\/api\/esm.html\" target=\"_blank\" rel=\"noopener\">Node.js ECMAScript modules documentation<\/a> and <a href=\"https:\/\/nodejs.org\/api\/packages.html\" target=\"_blank\" rel=\"noopener\">Node.js packages documentation<\/a> are excellent references.<\/p>\n\n<h2 id=\"practice\">Practice task<\/h2>\n<p>Build a tiny \u201cprice calculator\u201d split into modules:<\/p>\n<ol>\n<li>Create a folder <code>shop<\/code> and run <code>npm init -y<\/code> and <code>npm pkg set type=module<\/code>.<\/li>\n<li>Create <code>tax.js<\/code> with a named export <code>addGST(price, rate = 0.18)<\/code> that returns the price including 18% GST.<\/li>\n<li>Create <code>format.js<\/code> with a <strong>default export<\/strong> that formats a number as rupees, for example using <code>new Intl.NumberFormat(\"en-IN\", { style: \"currency\", currency: \"INR\" })<\/code>.<\/li>\n<li>Create <code>app.js<\/code> that imports both and prints the final price of 3 items.<\/li>\n<li>Bonus: add an old-style <code>discount.cjs<\/code> with <code>module.exports<\/code> and import it from <code>app.js<\/code>.<\/li>\n<\/ol>\n<p>If <code>node app.js<\/code> prints three nicely formatted prices, you have mastered the basics of Node.js modules.<\/p>\n\n<h2 id=\"faq\">FAQ<\/h2>\n<details><summary>Should I use ES Modules or CommonJS in a new Node.js project?<\/summary><p>Use ES Modules. They are the JavaScript standard, work in browsers too, support top-level <code>await<\/code>, and all current Node.js versions support them fully. Use CommonJS only when you maintain an older project or a tool that requires it.<\/p><\/details>\n<details><summary>What is the difference between .js, .mjs and .cjs?<\/summary><p><code>.mjs<\/code> is always an ES module, <code>.cjs<\/code> is always CommonJS, and <code>.js<\/code> follows the <code>\"type\"<\/code> field in the nearest <code>package.json<\/code> (<code>\"module\"<\/code> = ESM, missing or <code>\"commonjs\"<\/code> = CommonJS).<\/p><\/details>\n<details><summary>Can I use require() and import in the same file?<\/summary><p>Not directly. ESM files have no <code>require<\/code>, and CommonJS files cannot use the <code>import<\/code> statement. But ESM can use <code>import()<\/code> or <code>createRequire<\/code> from <code>node:module<\/code>, and CommonJS can use <code>require()<\/code> on ES modules (Node.js 22.12+) or <code>await import()<\/code>.<\/p><\/details>\n<details><summary>What is the difference between export default and named exports?<\/summary><p>A file can have many named exports, imported with curly braces and the same name. It can have only one default export, imported without braces under any name you like.<\/p><\/details>\n<details><summary>Why do I need the .js extension in imports?<\/summary><p>ES modules follow the same rules as browsers, where a URL must point to an exact file. Node.js does not guess extensions for relative ESM imports, so <code>.\/math<\/code> fails and <code>.\/math.js<\/code> works.<\/p><\/details>\n<details><summary>Does importing a module twice run it twice?<\/summary><p>No. A module runs once, the first time it is loaded. Later imports get the cached copy, so shared objects such as a database pool are created only once.<\/p><\/details>\n\n<div class=\"dd-next\"><strong>Next part:<\/strong> Node.js Part 3, <em>npm and package.json: Installing and Managing Packages<\/em>. You will install your first npm packages, understand <code>dependencies<\/code> vs <code>devDependencies<\/code>, version ranges, <code>package-lock.json<\/code> and npm scripts.<\/div>\n\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Node.js modules turn one big messy file into small, focused, reusable pieces. You learned the two systems: <strong>CommonJS<\/strong> with <code>require<\/code> and <code>module.exports<\/code>, and modern <strong>ES Modules<\/strong> with <code>import<\/code> and <code>export<\/code>. You know how Node.js picks between them (<code>.mjs<\/code>, <code>.cjs<\/code> and the <code>\"type\"<\/code> field), how to mix them, how to replace <code>__dirname<\/code>, load JSON and use dynamic <code>import()<\/code>, and why modules run only once. With this foundation, installing and using npm packages in the next part will feel natural.<\/p>\n\n<div class=\"dd-cta\">\n<h3>New posts every day on DevDojo<\/h3>\n<p>Follow the full <strong>Node.js series<\/strong>, from your first script to a production-ready API. Bookmark <a href=\"https:\/\/devdojo.co.in\/\">devdojo.co.in<\/a> and come back tomorrow for the next lesson.<\/p>\n<\/div>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Learn Node.js modules step by step: ES Modules vs CommonJS, import and export, require and module.exports, .mjs vs .cjs, and how to mix both safely.<\/p>\n","protected":false},"author":1,"featured_media":271,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,8],"tags":[143,142,83,43,127,126],"class_list":["post-272","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-backend","category-nodejs","tag-commonjs","tag-es-modules","tag-javascript","tag-node-js","tag-node-js-for-beginners","tag-series-node-js"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.4 - aioseo.com -->\n\t<meta name=\"description\" content=\"Learn Node.js modules step by step: ES Modules vs CommonJS, import and export, require and module.exports, .mjs vs .cjs, and how to mix both safely.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Lokendra\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.4\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"DevDojo - Empowering Developers\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Node.js Modules: ES Modules vs CommonJS Explained\" \/>\n\t\t<meta property=\"og:description\" content=\"Learn Node.js modules step by step: ES Modules vs CommonJS, import and export, require and module.exports, .mjs vs .cjs, and how to mix both safely.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-10-30T13:06:11+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-10-30T13:06:12+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Node.js Modules: ES Modules vs CommonJS Explained\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Learn Node.js modules step by step: ES Modules vs CommonJS, import and export, require and module.exports, .mjs vs .cjs, and how to mix both safely.\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/30\\\/nodejs-series-part-2-modules-esm-commonjs\\\/#blogposting\",\"name\":\"Node.js Modules: ES Modules vs CommonJS Explained\",\"headline\":\"Node.js Part 2: Modules in Node.js: ES Modules, CommonJS and import\\\/export\",\"author\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/nodejs-series-part-2-modules-esm-commonjs.jpg\",\"width\":1200,\"height\":630,\"caption\":\"Node.js Part 2 banner: Node.js modules, ES Modules vs CommonJS with import, export and require\"},\"datePublished\":\"2026-10-30T13:06:11+00:00\",\"dateModified\":\"2026-10-30T13:06:12+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/30\\\/nodejs-series-part-2-modules-esm-commonjs\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/30\\\/nodejs-series-part-2-modules-esm-commonjs\\\/#webpage\"},\"articleSection\":\"Backend, NodeJS, CommonJS, ES Modules, JavaScript, Node.js, Node.js for Beginners, Series: Node.js\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/30\\\/nodejs-series-part-2-modules-esm-commonjs\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/devdojo.co.in\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/backend\\\/#listItem\",\"name\":\"Backend\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/backend\\\/#listItem\",\"position\":2,\"name\":\"Backend\",\"item\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/backend\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/backend\\\/nodejs\\\/#listItem\",\"name\":\"NodeJS\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/backend\\\/nodejs\\\/#listItem\",\"position\":3,\"name\":\"NodeJS\",\"item\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/backend\\\/nodejs\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/30\\\/nodejs-series-part-2-modules-esm-commonjs\\\/#listItem\",\"name\":\"Node.js Part 2: Modules in Node.js: ES Modules, CommonJS and import\\\/export\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/backend\\\/#listItem\",\"name\":\"Backend\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/30\\\/nodejs-series-part-2-modules-esm-commonjs\\\/#listItem\",\"position\":4,\"name\":\"Node.js Part 2: Modules in Node.js: ES Modules, CommonJS and import\\\/export\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/backend\\\/nodejs\\\/#listItem\",\"name\":\"NodeJS\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\",\"name\":\"DevDojo\",\"description\":\"Empowering Developers\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/\",\"name\":\"Lokendra\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/30\\\/nodejs-series-part-2-modules-esm-commonjs\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/99f102f0cc9080237ce54b91304fd1b3b6fa29925cbaeba93cd796872e58b45c?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Lokendra\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/30\\\/nodejs-series-part-2-modules-esm-commonjs\\\/#webpage\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/30\\\/nodejs-series-part-2-modules-esm-commonjs\\\/\",\"name\":\"Node.js Modules: ES Modules vs CommonJS Explained\",\"description\":\"Learn Node.js modules step by step: ES Modules vs CommonJS, import and export, require and module.exports, .mjs vs .cjs, and how to mix both safely.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/30\\\/nodejs-series-part-2-modules-esm-commonjs\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/nodejs-series-part-2-modules-esm-commonjs.jpg\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/30\\\/nodejs-series-part-2-modules-esm-commonjs\\\/#mainImage\",\"width\":1200,\"height\":630,\"caption\":\"Node.js Part 2 banner: Node.js modules, ES Modules vs CommonJS with import, export and require\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/30\\\/nodejs-series-part-2-modules-esm-commonjs\\\/#mainImage\"},\"datePublished\":\"2026-10-30T13:06:11+00:00\",\"dateModified\":\"2026-10-30T13:06:12+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#website\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/\",\"name\":\"DevDojo\",\"description\":\"Empowering Developers\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Node.js Modules: ES Modules vs CommonJS Explained","description":"Learn Node.js modules step by step: ES Modules vs CommonJS, import and export, require and module.exports, .mjs vs .cjs, and how to mix both safely.","canonical_url":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/#blogposting","name":"Node.js Modules: ES Modules vs CommonJS Explained","headline":"Node.js Part 2: Modules in Node.js: ES Modules, CommonJS and import\/export","author":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"publisher":{"@id":"https:\/\/devdojo.co.in\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/devdojo.co.in\/wp-content\/uploads\/2026\/10\/nodejs-series-part-2-modules-esm-commonjs.jpg","width":1200,"height":630,"caption":"Node.js Part 2 banner: Node.js modules, ES Modules vs CommonJS with import, export and require"},"datePublished":"2026-10-30T13:06:11+00:00","dateModified":"2026-10-30T13:06:12+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/#webpage"},"isPartOf":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/#webpage"},"articleSection":"Backend, NodeJS, CommonJS, ES Modules, JavaScript, Node.js, Node.js for Beginners, Series: Node.js"},{"@type":"BreadcrumbList","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/#listItem","position":1,"name":"Home","item":"https:\/\/devdojo.co.in\/","nextItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/backend\/#listItem","name":"Backend"}},{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/backend\/#listItem","position":2,"name":"Backend","item":"https:\/\/devdojo.co.in\/index.php\/category\/backend\/","nextItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/backend\/nodejs\/#listItem","name":"NodeJS"},"previousItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/backend\/nodejs\/#listItem","position":3,"name":"NodeJS","item":"https:\/\/devdojo.co.in\/index.php\/category\/backend\/nodejs\/","nextItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/#listItem","name":"Node.js Part 2: Modules in Node.js: ES Modules, CommonJS and import\/export"},"previousItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/backend\/#listItem","name":"Backend"}},{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/#listItem","position":4,"name":"Node.js Part 2: Modules in Node.js: ES Modules, CommonJS and import\/export","previousItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/backend\/nodejs\/#listItem","name":"NodeJS"}}]},{"@type":"Organization","@id":"https:\/\/devdojo.co.in\/#organization","name":"DevDojo","description":"Empowering Developers","url":"https:\/\/devdojo.co.in\/"},{"@type":"Person","@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author","url":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/","name":"Lokendra","image":{"@type":"ImageObject","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/99f102f0cc9080237ce54b91304fd1b3b6fa29925cbaeba93cd796872e58b45c?s=96&d=mm&r=g","width":96,"height":96,"caption":"Lokendra"}},{"@type":"WebPage","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/#webpage","url":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/","name":"Node.js Modules: ES Modules vs CommonJS Explained","description":"Learn Node.js modules step by step: ES Modules vs CommonJS, import and export, require and module.exports, .mjs vs .cjs, and how to mix both safely.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/devdojo.co.in\/#website"},"breadcrumb":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/#breadcrumblist"},"author":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"creator":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"image":{"@type":"ImageObject","url":"https:\/\/devdojo.co.in\/wp-content\/uploads\/2026\/10\/nodejs-series-part-2-modules-esm-commonjs.jpg","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/#mainImage","width":1200,"height":630,"caption":"Node.js Part 2 banner: Node.js modules, ES Modules vs CommonJS with import, export and require"},"primaryImageOfPage":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/#mainImage"},"datePublished":"2026-10-30T13:06:11+00:00","dateModified":"2026-10-30T13:06:12+00:00"},{"@type":"WebSite","@id":"https:\/\/devdojo.co.in\/#website","url":"https:\/\/devdojo.co.in\/","name":"DevDojo","description":"Empowering Developers","inLanguage":"en-US","publisher":{"@id":"https:\/\/devdojo.co.in\/#organization"}}]},"og:locale":"en_US","og:site_name":"DevDojo - Empowering Developers","og:type":"article","og:title":"Node.js Modules: ES Modules vs CommonJS Explained","og:description":"Learn Node.js modules step by step: ES Modules vs CommonJS, import and export, require and module.exports, .mjs vs .cjs, and how to mix both safely.","og:url":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/","article:published_time":"2026-10-30T13:06:11+00:00","article:modified_time":"2026-10-30T13:06:12+00:00","twitter:card":"summary_large_image","twitter:title":"Node.js Modules: ES Modules vs CommonJS Explained","twitter:description":"Learn Node.js modules step by step: ES Modules vs CommonJS, import and export, require and module.exports, .mjs vs .cjs, and how to mix both safely."},"aioseo_meta_data":{"post_id":"272","title":"Node.js Modules: ES Modules vs CommonJS Explained","description":"Learn Node.js modules step by step: ES Modules vs CommonJS, import and export, require and module.exports, .mjs vs .cjs, and how to mix both safely.","keywords":null,"keyphrases":{"focus":{"keyphrase":"Node.js modules"},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":"Node.js Modules: ES Modules vs CommonJS Explained","og_description":"Learn Node.js modules step by step: ES Modules vs CommonJS, import and export, require and module.exports, .mjs vs .cjs, and how to mix both safely.","og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":0,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":{"faqs":[],"keyPoints":[],"schemas":[],"titles":[],"descriptions":[],"socialPosts":{"email":{"subject":"","preview":"","content":""},"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"created":"2026-10-30 12:59:21","updated":"2026-10-30 13:06:12","focus_keyword":"Node.js modules","additional_keywords":null,"truseo_locale":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/devdojo.co.in\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/devdojo.co.in\/index.php\/category\/backend\/\" title=\"Backend\">Backend<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/devdojo.co.in\/index.php\/category\/backend\/nodejs\/\" title=\"NodeJS\">NodeJS<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tNode.js Part 2: Modules in Node.js: ES Modules, CommonJS and import\/export\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/devdojo.co.in\/"},{"label":"Backend","link":"https:\/\/devdojo.co.in\/index.php\/category\/backend\/"},{"label":"NodeJS","link":"https:\/\/devdojo.co.in\/index.php\/category\/backend\/nodejs\/"},{"label":"Node.js Part 2: Modules in Node.js: ES Modules, CommonJS and import\/export","link":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/30\/nodejs-series-part-2-modules-esm-commonjs\/"}],"_links":{"self":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/comments?post=272"}],"version-history":[{"count":1,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/272\/revisions"}],"predecessor-version":[{"id":275,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/272\/revisions\/275"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/media\/271"}],"wp:attachment":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/media?parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/categories?post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/tags?post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}