<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Khushal Patil on Medium]]></title>
        <description><![CDATA[Stories by Khushal Patil on Medium]]></description>
        <link>https://medium.com/@khushalpatil033?source=rss-fa6f4218bfc2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1n7kLaSPSyviLANGKEtZ7w.jpeg</url>
            <title>Stories by Khushal Patil on Medium</title>
            <link>https://medium.com/@khushalpatil033?source=rss-fa6f4218bfc2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 10:03:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@khushalpatil033/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Universal JavaScript Library with NestJS for Frontend and Backend]]></title>
            <link>https://javascript.plainenglish.io/building-a-universal-javascript-library-with-nestjs-for-frontend-and-backend-c14373751fa2?source=rss-fa6f4218bfc2------2</link>
            <guid isPermaLink="false">https://medium.com/p/c14373751fa2</guid>
            <category><![CDATA[backend-library]]></category>
            <category><![CDATA[cjs-and-esm]]></category>
            <category><![CDATA[nestjs]]></category>
            <category><![CDATA[frontend-libraries]]></category>
            <category><![CDATA[universal-library]]></category>
            <dc:creator><![CDATA[Khushal Patil]]></dc:creator>
            <pubDate>Mon, 31 Mar 2025 08:54:47 GMT</pubDate>
            <atom:updated>2025-04-03T05:11:28.408Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AdpYvM9FRrWmyzRBTxj_1g.png" /></figure><h3>Introduction</h3><p>JavaScript libraries are usually built <strong>either</strong> for frontend or backend. But what if we could create a <strong>single</strong> library that seamlessly works in both environments?</p><p>With <strong>NestJS</strong>, a powerful Node.js framework, we can build a <strong>universal JavaScript library</strong> that supports both:</p><p>✅ <strong>CommonJS (CJS)</strong> — Used in backend (Node.js)<br> ✅ <strong>ECMAScript Modules (ESM)</strong> — Used in modern frontend frameworks</p><p>In this guide, we’ll <strong>build, structure, and publish</strong> a <strong>NestJS-based library</strong> that works in both frontend and backend environments.</p><h3>Why Use NestJS for a Universal Library?</h3><p>NestJS is a <strong>TypeScript-based</strong> framework built on top of Express (or Fastify). While it’s primarily used for backend development, its <strong>modular architecture</strong> and <strong>dependency injection</strong> system make it <strong>perfect</strong> for creating reusable libraries.</p><h3>Benefits of Using NestJS for a Library:</h3><p>✔ <strong>Code Reusability</strong> — Use the same logic across frontend &amp; backend.<br> ✔ <strong>TypeScript Support</strong> — Ensures <strong>type safety</strong> and <strong>better maintainability</strong>.<br> ✔ <strong>Built-in Dependency Injection</strong> — Makes modules and services easy to reuse.<br> ✔ <strong>Supports Both CJS &amp; ESM</strong> — Works with multiple JavaScript module systems.</p><h3>Setting Up the Project</h3><h3>1. Initialize a New NestJS Library</h3><p>First, create a new NestJS project:</p><pre>npx @nestjs/cli new universal-library<br>cd universal-library</pre><p>Now, generate a <strong>module and service</strong> inside the project:</p><pre>nest generate module modules/math-utils<br>nest generate service modules/math-utils</pre><h3>Creating the Universal Module</h3><p>Inside /src/modules/math-utils/math-utils.service.ts, define a simple <strong>utility service</strong> that works for both frontend and backend:</p><pre>import { Injectable } from &#39;@nestjs/common&#39;;</pre><pre>@Injectable()<br>export class MathUtilsService {<br>  static add(a: number, b: number): number {<br>    return a + b;<br>  }<br>}</pre><p>💡 <strong>Important:</strong><br> Since this library is <strong>meant for both frontend and backend</strong>, <strong>avoid</strong> using <strong>Node-specific</strong> modules like fs, path, or winston.</p><h3>2. Export the Module</h3><p>Modify or add src/index.ts to export the utility:</p><pre>export * from &#39;./modules/math-utils/math-utils.service&#39;;</pre><h3>Supporting Both CJS and ESM</h3><p>To support <strong>both module formats</strong>, we need two TypeScript configurations:</p><h3>📌 Create tsconfig.cjs.json</h3><pre>{<br>  &quot;extends&quot;: &quot;./tsconfig.json&quot;,<br>  &quot;compilerOptions&quot;: {<br>    &quot;module&quot;: &quot;CommonJS&quot;,<br>    &quot;outDir&quot;: &quot;./dist/cjs/src&quot;<br>  },<br>  &quot;exclude&quot;: [&quot;node_modules&quot;, &quot;test&quot;, &quot;dist&quot;]<br>}</pre><h3>📌 Create tsconfig.esm.json</h3><pre>{<br>  &quot;extends&quot;: &quot;./tsconfig.json&quot;,<br>  &quot;compilerOptions&quot;: {<br>    &quot;module&quot;: &quot;ESNext&quot;,<br>    &quot;outDir&quot;: &quot;./dist/esm/src&quot;<br>  },<br>  &quot;exclude&quot;: [&quot;node_modules&quot;, &quot;test&quot;, &quot;dist&quot;]<br>}</pre><h3>🔧 Update package.json</h3><p>Modify package.json to handle both <strong>CJS</strong> and <strong>ESM</strong> builds:</p><pre>&quot;scripts&quot;: {<br>  &quot;build:cjs&quot;: &quot;tsc -p tsconfig.cjs.json&quot;,<br>  &quot;build:esm&quot;: &quot;tsc -p tsconfig.esm.json&quot;,<br>  &quot;build&quot;: &quot;rm -rf dist &amp;&amp; npm run build:cjs &amp;&amp; npm run build:esm&quot;<br>}</pre><h3>🚀 Build the Library</h3><p>Run the following command to generate <strong>both CJS and ESM outputs</strong>:</p><pre>npm run build</pre><h3>Deploying the Library</h3><p>Once the library is <strong>built</strong>, there are <strong>two ways</strong> to deploy it:</p><h3>1️⃣ Publishing to npm</h3><p>This allows developers to install your package via npm install.</p><h4>📌 Steps to Publish:</h4><p>1️⃣ <strong>Login to npm</strong></p><pre>npm login</pre><p>2️⃣ <strong>Publish the package</strong></p><pre>npm publish --access public</pre><p>3️⃣ <strong>Now, anyone can install it using:</strong></p><pre>npm install math-utils</pre><h3>2️⃣ Deploying via GitHub or Bitbucket</h3><p>If you <strong>don’t want to publish on npm</strong>, you can distribute it via <strong>GitHub/Bitbucket</strong>.</p><h4>📌 Steps to Deploy:</h4><p>1️⃣ <strong>Commit all changes</strong></p><pre>git add .<br>git commit -m &quot;Initial release&quot;</pre><p>2️⃣ <strong>Create a version tag</strong></p><pre>git tag v1.0.0</pre><p>3️⃣ <strong>Push the tag to the repository</strong></p><pre>git push origin v1.0.0</pre><p>4️⃣ <strong>Users can now install it via GitHub:</strong></p><pre>npm install git+https://github.com/your-org/shared-utils.git#v1.0.0</pre><p>Alternatively, add this in package.json:</p><pre>&quot;dependencies&quot;: {<br>  &quot;math-utils&quot;: &quot;git+https://github.com/your-org/shared-utils.git#v1.0.0&quot;<br>}</pre><h3>Conclusion</h3><p>By leveraging <strong>NestJS</strong>, we created a <strong>universal JavaScript library</strong> that:<br> ✅ <strong>Works in both frontend &amp; backend</strong> (CJS + ESM)<br> ✅ <strong>Supports TypeScript for better maintainability</strong><br> ✅ <strong>Can be deployed via npm or GitHub</strong></p><p>This approach helps <strong>reduce code duplication</strong>, <strong>improve maintainability</strong>, and <strong>streamline full-stack development</strong>.</p><p><strong>Thinking about building your own universal JavaScript library?</strong> 💡 Let us know your thoughts in the comments! 🚀</p><h3>Thank you for being a part of the community</h3><p><em>Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://x.com/inPlainEngHQ"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/@InPlainEnglish"><strong>YouTube</strong></a> | <a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a> | <a href="https://open.spotify.com/show/7qxylRWKhvZwMz2WuEoua0"><strong>Podcast</strong></a> | <a href="https://differ.blog/inplainenglish"><strong>Differ</strong></a> | <a href="https://twitch.tv/inplainenglish"><strong>Twitch</strong></a></li><li><a href="https://cofeed.app/"><strong>Check out CoFeed, the smart way to stay up-to-date with the latest in tech</strong></a> <strong>🧪</strong></li><li><a href="https://differ.blog/"><strong>Start your own free AI-powered blog on Differ</strong></a> 🚀</li><li><a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Join our content creators community on Discord</strong></a> 🧑🏻‍💻</li><li>For more content, visit <a href="https://plainenglish.io/"><strong>plainenglish.io</strong></a> + <a href="https://stackademic.com/"><strong>stackademic.com</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c14373751fa2" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/building-a-universal-javascript-library-with-nestjs-for-frontend-and-backend-c14373751fa2">Building a Universal JavaScript Library with NestJS for Frontend and Backend</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>