<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Faik Djikic</title>
    <description>The latest articles on DEV Community by Faik Djikic (@faikdjikic).</description>
    <link>https://dev.to/faikdjikic</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F722411%2Fa8fd0d69-3342-45a2-8efe-95b180ab0167.jpeg</url>
      <title>DEV Community: Faik Djikic</title>
      <link>https://dev.to/faikdjikic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/faikdjikic"/>
    <language>en</language>
    <item>
      <title>Default and named exports in node.js</title>
      <dc:creator>Faik Djikic</dc:creator>
      <pubDate>Thu, 28 Mar 2024 22:39:14 +0000</pubDate>
      <link>https://dev.to/faikdjikic/default-and-named-exports-in-nodejs-2k7n</link>
      <guid>https://dev.to/faikdjikic/default-and-named-exports-in-nodejs-2k7n</guid>
      <description>&lt;p&gt;As an "old fart" programmer I tend to write a lot of reusable code and improve it with each usage. Eventually, now and then, I encounter compatibility issues, and although it is on a small scale (usually, the code is used only by my team and me) it still gets to be a PitA. One of those was expanding a commonly used node module that exports a single function as default:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports=function(){
console.log("hello");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to multi-function module with named exports like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fn1=function(){
console.log("hello");
}
const fn2=function(){
console.log("hi");
}
const fn3=function(){
console.log("howdy");
}
module.exports={fn1,fn2,fn3}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Simple and clean expansion, but one that would break compatibility with every other module that used this expecting a default export:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const greeting=require('./hello');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I solved it using a rather unorthodox approach and it seems to be working (tested down to version 16.20.2 and up to 21.5.0)&lt;/p&gt;

&lt;p&gt;Here's the module code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fn1 = function () {
    console.log("hello");
}
const fn2 = function () {
    console.log("hi");
}
const fn3 = function () {
    console.log("howdy");
}
fn1.fn1 = fn1;
fn1.fn2 = fn2;
fn1.fn3 = fn3;
module.exports = fn1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and here's the implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const greeting = require('./hello');
const { fn1, fn2, fn3 } = require('./hello');
greeting();
fn3();
fn1();
fn2();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works perfectly and I'm not aware of any downsides. I would appreciate any suggestion on how this could be wrong or potentially dangerous before I implement it in production.&lt;/p&gt;

&lt;p&gt;On the other hand, if it is safe to use, it might be a useful approach in similar situations&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
