<?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"><channel><title><![CDATA[DevHelp Hub]]></title><description><![CDATA[a devoted web developer and coding enthusiast.]]></description><link>https://devhelphub.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 30 Aug 2026 16:22:02 GMT</lastBuildDate><atom:link href="https://devhelphub.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[A Comprehensive Step-By-Step Guide on Integrating TailwindCSS with NodeJS, Express and EJS]]></title><description><![CDATA[In this tutorial, you will learn how to integrate TailwindCSS with Node.js, Express, and EJS.

Project setup


The first step is to create and set up our application.
Run the following commands to create the nodeJS directory and change the current wo...]]></description><link>https://devhelphub.hashnode.dev/a-comprehensive-step-by-step-guide-on-integrating-tailwindcss-with-nodejs-express-and-ejs</link><guid isPermaLink="true">https://devhelphub.hashnode.dev/a-comprehensive-step-by-step-guide-on-integrating-tailwindcss-with-nodejs-express-and-ejs</guid><category><![CDATA[Tailwind CSS]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[Express]]></category><category><![CDATA[ejs]]></category><category><![CDATA[CSS]]></category><dc:creator><![CDATA[Muhammad Faizan Soomro]]></dc:creator><pubDate>Sun, 24 Sep 2023 19:52:01 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/mZnx9429i94/upload/4fecb56471aea5e295ced4939625edea.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this tutorial, you will learn how to integrate TailwindCSS with Node.js, Express, and EJS.</p>
<ol>
<li><h3 id="heading-project-setup">Project setup</h3>
</li>
</ol>
<p>The first step is to create and set up our application.</p>
<p>Run the following commands to create the nodeJS directory and change the current working directory to it:</p>
<pre><code class="lang-powershell">mkdir nodeJS
<span class="hljs-built_in">cd</span> nodeJS
</code></pre>
<p>Now we need to initialize our Node project. To do so, execute the following command:</p>
<pre><code class="lang-powershell">npm init
</code></pre>
<p>The objective of npm init is to generate the package.json file which is used to contain project metadata (e.g., name, description, version, author, etc.) and to manage project dependencies and scripts.</p>
<p>When you run the program, you will be presented with an interactive prompt where you may enter details about your project. If you are satisfied with the suggested information, hit enter until the end.</p>
<p>Alternatively, you can run:</p>
<pre><code class="lang-powershell">npm init <span class="hljs-literal">-y</span>
</code></pre>
<p>Here the -y flag is equal to you pressing enter until the end without adding any information i.e. going with the default settings.</p>
<ol>
<li><h3 id="heading-enabling-ecmascript-modules-in-nodejs"><strong>Enabling ECMAScript modules in Node.js</strong></h3>
</li>
</ol>
<p>The ECMAScript modules (in short ES modules) is a JavaScript modules format that uses import and export statements. The default format of modules in Node.js is CommonJS. To enable ES modules, add the following line in the package.json file.</p>
<pre><code class="lang-json"><span class="hljs-string">"type"</span>: <span class="hljs-string">"module"</span>
</code></pre>
<ol>
<li><h3 id="heading-installing-all-dependencies">Installing all dependencies</h3>
</li>
</ol>
<p>Run the following command to install all dependencies:</p>
<pre><code class="lang-powershell">npm install express ejs; npm install <span class="hljs-literal">-D</span> tailwindcss
</code></pre>
<ol>
<li><h3 id="heading-setting-up-tailwindcss">Setting up TailwindCSS</h3>
</li>
</ol>
<p>Create a new directory and files for CSS.</p>
<pre><code class="lang-powershell">On Mac : mkdir public; mkdir public/styles; touch public/styles/input.css; touch public/styles/style.css
On Windows : mkdir public; mkdir public/styles; <span class="hljs-built_in">ni</span> public/styles/input.css; <span class="hljs-built_in">ni</span> public/styles/style.css
</code></pre>
<p>The TailwindCSS is stored in the input.css file, while the final CSS used by our website is stored in the style.css file.</p>
<p>Add the following code in input.css</p>
<pre><code class="lang-css"><span class="hljs-keyword">@tailwind</span> base;
<span class="hljs-keyword">@tailwind</span> components;
<span class="hljs-keyword">@tailwind</span> utilities;
</code></pre>
<p>The code above injects TailwindCSS's base, components, and utility styles into our CSS. At runtime, these directives are transformed into real CSS.</p>
<ol>
<li><h3 id="heading-setting-up-ejs-file">Setting up EJS file</h3>
</li>
</ol>
<p>Create a new directory, and an EJS file, as follows:</p>
<pre><code class="lang-powershell">On Mac : mkdir views; touch views/index.ejs
On Windows : mkdir views; <span class="hljs-built_in">ni</span> views/index.ejs
</code></pre>
<p><em><mark>NOTE: make sure to name the folder as ‘views’ because by default express is going to look for the folder with this name.</mark></em></p>
<p>Now add content to your index.ejs file</p>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/styles/style.css"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Share<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-3xl text-purple-700"</span>&gt;</span>KEEP CODING<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<ol>
<li><h3 id="heading-creating-basic-server">Creating basic server</h3>
</li>
</ol>
<p>Create the index.js file, which contains our basic server.</p>
<p>You can do it manually, but it will look more cool if you use the terminal :D (I would recommend using the terminal to tighten your grip on it).</p>
<pre><code class="lang-powershell">On Mac : touch index.js
On Windows : <span class="hljs-built_in">ni</span> index.js
</code></pre>
<p>Now create the server and set up static files. Copy the following code in your index.js</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> express <span class="hljs-keyword">from</span> <span class="hljs-string">"express"</span>;
<span class="hljs-keyword">const</span> app = express();
<span class="hljs-keyword">const</span> port = <span class="hljs-number">3000</span>;
app.use(express.static(<span class="hljs-string">"public"</span>));
app.get(<span class="hljs-string">"/"</span>,<span class="hljs-function">(<span class="hljs-params">req,res</span>)=&gt;</span>{
    res.render(<span class="hljs-string">"index.ejs"</span>);
})
app.listen(port);
</code></pre>
<ol>
<li><h3 id="heading-generating-tailwindcss-configuration-file">Generating TailwindCSS configuration file</h3>
</li>
</ol>
<pre><code class="lang-powershell">npx tailwindcss init
</code></pre>
<p>The tailwind.config.js file is created as a result of this. This file's purpose is to allow you to personalize your TailwindCSS installation. It's a configuration file where you may add extra information like plugins, themes, margins, padding, and whatever else Tailwind doesn't offer.</p>
<p>Now add the paths to all of your ejs files in your tailwind.config.js file. Copy the following code in your tailwind.config.js file.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">/** <span class="hljs-doctag">@type <span class="hljs-type">{import('tailwindcss').Config}</span> </span>*/</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
  <span class="hljs-attr">content</span>: [<span class="hljs-string">"./views/*"</span>],
  <span class="hljs-attr">theme</span>: {
    <span class="hljs-attr">extend</span>: {},
  },
  <span class="hljs-attr">plugins</span>: [],
}
</code></pre>
<ol>
<li><h3 id="heading-last-step">Last Step:</h3>
</li>
</ol>
<p>In your package.json file under scripts, add the following line:</p>
<pre><code class="lang-json"><span class="hljs-string">"css"</span>: <span class="hljs-string">"tailwindcss -i ./public/styles/input.css -o ./public/styles/style.css --watch"</span>
</code></pre>
<p>With the above command, we build the CSS and add it to the styles.css file, which is used by our website.</p>
<h2 id="heading-how-to-run">How to run?</h2>
<p>Run the following code in your terminal:</p>
<pre><code class="lang-powershell">npm run css
</code></pre>
<p>Then open another terminal without closing the previous one and run your server.</p>
<pre><code class="lang-powershell">nodemon index.js
</code></pre>
<p><mark>NOTE: assuming that you know about nodemon, otherwise visit </mark> <a target="_blank" href="https://www.npmjs.com/package/nodemon"><mark>here.</mark></a></p>
<p>Finally, open the website <a target="_blank" href="http://localhost:3000">localhost:3000</a> and your website is READY!</p>
<p>Now, just leave the second terminal as it is and go back to the first one. Open your index.ejs file and make any changes to the style of H1 and save it. You will see some updation in the terminal just ignore it and refresh your website. BOOM NEW STYLES HAVE BEEN ADDED!</p>
<h1 id="heading-conclusion">Conclusion:</h1>
<p>You should now have a fully functional Node.js application using Express, Pug, and TailwindCSS.</p>
]]></content:encoded></item></channel></rss>