Using tailwind in conjunction with ExpressJS and EJS template: a step-by-step guide

I am facing a challenge in incorporating Tailwind CSS into my ExpressJS project using EJS as the view engine. Could anyone provide guidance on how to resolve this issue?

Below is a snippet from my app.js file:

require("dotenv").config();
const express = require("express");
const ejs = require("express");
const bodyParser = require("body-parser");
const port = 3000;
const dotenv = require("dotenv");
const app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "src"));

app.get("/", (req, res) => {
res.render("homepage");
});

app.listen(port, (req, res) => {
console.log("Server running");
});

In my homepage.ejs file, I have the following content:

<!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" 
 />
    <link href="/dist/output.css" rel="stylesheet" />
    <title>Document</title>
  </head>
  <body>
    <h1 class="text-3xl font-bold underline">Hello world!</h1>
  </body>
 </html>

The server is up and running smoothly. To generate the Tailwind CSS file, I used the command:

npx tailwindcss -i ./src/style.css -o ./dist/output.css --watch

This is the structure of my project directory:

Project/
    -- node_modules
    -- src/
       -- index.html
       -- style.css
     -- views/
        -- homepage.ejs

For Tailwind CSS configuration, here is my tailwind.config.js setup:

module.exports = {
  content: [
    "./src/*.{html,js,css} ",
    "./views/homepage.ejs",
  ],
  theme: {
    extend: {},
  },
  plugins: [
    {
      tailwindcss: {},
      autoprefixer: {},
    },
  ],
};

https://i.sstatic.net/lOcaq.png

Here is a screenshot of the homepage layout.

Answer №1

It seems like the issue lies in not exposing the dist folder in your Express app.

You have exposed the src folder but not the dist folder where your styles are located.

In addition, you have exposed the src folder but not the views! Your Express app may be displaying the index.html page from the src folder instead of rendering the EJS templates.

Based on a demo repository using Pug found here, consider making these changes to your Express entry point:

require("dotenv").config();
const express = require("express");
const ejs = require("express");
const bodyParser = require("body-parser");
const port = 3000;
const dotenv = require("dotenv");
const app = express();
const path = require("path");

app.use(bodyParser.urlencoded({ extended: true }));

// => Expose the views directory for rendering
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// => Expose the dist folder for serving static files
app.use(express.static(path.join(__dirname, 'dist')))

app.get("/", (req, res) => {
  res.render("homepage");
});

app.listen(port, (req, res) => {
  console.log("Server running");
});

Don't forget to link the CSS in your view file using

<link href="/output.css" rel="stylesheet" />

If you plan to add more views in the future, consider adding a wildcard to include every .ejs file in the views folder:

module.exports = {
  content: [
    "./src/*.{html,js,css}",
    "./views/*.ejs",
  ],
  theme: {
    extend: {},
  },
  plugins: [
    {
      tailwindcss: {},
      autoprefixer: {},
    },
  ],
};

For further guidance, you can refer to this detailed article: . Although it's written for Pug, a simple substitution of pug with ejs should suffice.

I hope this information proves helpful!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Cypress: A guide to storing CSS color values in a variable

I'm struggling to figure out how to save a CSS color value to a variable using Cypress in my React application. I have a good understanding of the assertions, such as: cy.get('myElement').should('have.css', 'color').and ...

Utilize CSS inheritance within Vue components

My setup involves two components that each have child components in a router configuration. Here's an example of how it looks: { path: '/admin', component: AdminMain, children:[ { path: &a ...

What is the best way to choose all cells that begin, include, or finish with a specific term using JQuery/CSS?

I have a table with some cells and I am looking to utilize JQuery to select specific cells. For example: <td>John Doe</td> <td>John Doe1</td> <td>1John Doe</td> I want to select cells that start with 1, include Doe, a ...

Unable to delete borders within the container

Is there a way to eliminate the borders within this container? Visit this link for reference. I've tried using firebug but I can't seem to locate it... Appreciate any assistance you can provide. Thank you! ...

Configure Node.js to serve static Angular pages post deployment

Just starting to learn nodejs. I have an API folder where the server should execute a function when receiving an API request. However, for any non-API requests, I want the nodejs server to serve static files so that the angular app can handle displaying th ...

The directive call triggers the loading of data from MongoDB

I'm currently working on a small invoice demo application using Angular. The data is stored in a MongoDB and is called in a controller named invoiceCtrl. Here's how the controller looks: invCon.controller( 'invoiceCtrl', ['$http&a ...

Challenges with creating a responsive YouTube iframe using the API

Looking to integrate a Youtube video using the iFrame API to capture events effectively, embedding the player alone is not an option. Following the documentation, everything functions correctly with code like: var tag = document.createElement('scrip ...

What is the best way to align a value within CardActions in Material UI?

I'm currently facing an issue with my website design. Here's a snapshot of how it looks: https://i.sstatic.net/UuBJJ.png Additionally, here is the code snippet related to the problem: <CardActions> <Typography style={{ fon ...

What is the best way to upload a file in Node.js using Express and Multer?

When attempting to send a file from the front end to my node js server, I encountered an issue with receiving the file on the back end. Here is the code snippet: <form id="file-upload-form" class="uploader" action="/uploa ...

What is the best way to keep a <div> class anchored to the bottom of an HTML page?

I am looking to incorporate a footer into my website that stays fixed at the bottom of the screen. However, I have encountered an issue: Here is what I have attempted so far: .footer { position: absolute; width: 100%; background: #0084FF; ...

How do I stop the label element from interfering with the corners of my search bar from rounding

Issue I am experiencing an issue with the appearance of my search bar when using a label element. The corners on the left-hand side are not rounded, and the alignment of the search button is off. When I remove the label element, the search bar looks perf ...

The utilization of Display Flex resulting in the enlargement of the containing div

I am currently learning CSS and trying out some new things. I am looking to create a page with two side-by-side divs: one for the sidebar and the other for the main content. My goal is to have the sidebar take up 400px of width and allow the content part t ...

Node Package Manager (NPM): The main entry point to the heart of the

I am currently in the process of developing a custom NPM package. This package requires a module from the user's application root directory, specifically a properties module that is placed there by the user themselves. My main question is: How can I ...

Aligning an image alongside two separate blocks of text

I'm having trouble creating a header bar on my HTML page with a h1 and h2 element, along with an image positioned to the right of both. No matter what I try, the image keeps appearing below the other elements. I attempted using display:inline-block; b ...

Issues with the 'GET' request functionality on the deployed Angular project

I am attempting to run an Angular project that I have built. After copying the folder generated from 'ng build' and placing it in the directory where my back end code (using express) is located, I am trying to run it on my laptop at port 3000. Wh ...

The footer should always be anchored at the bottom of the webpage, maintaining a consistent position regardless of any changes to the browser's

I've successfully implemented a footer with several buttons that remains positioned at the bottom of the page, 60px above the very bottom, regardless of the content or window size. The CSS I'm using is as follows: #container { min-height: 10 ...

Setting Collation and Charset in Node.js Models using Sequelize and Mysql

Currently, I am utilizing sequelize with node and node-mysql in my project. When creating models using the sequelize-cli, the resulting code appears as follows: 'use strict'; module.exports = function(sequelize, DataTypes) { let songs = seq ...

The act of resizing a window causes the text to be forcibly expelled from its containing div

When you hover over an image in a grid, text and a button appear. However, when the window is resized, the content ends up overflowing out of the div. This issue is part of my first project that I am working on during a bootcamp. I have decided to keep th ...

Express does not seem to support Http2

In my server.js file, there is some code as follows: var express = require('express') var app = express() var fs = require('fs'); app.get('/', function (req, res) { res.send('hello, http2!') }) var options = { ...

What could be causing a etimedout error with Node.js Active Directory integration?

As I navigate through the node js express framework, a notable issue arises with my Active Directory connection resulting in the error message below: "code":"ETIMEDOUT","errno":"ETIMEDOUT","syscall":"connect" The code I've implemented to access th ...