DEV Community

Cover image for How to use Tailwind CSS in an ExpressJS app.
Jumbo Daniel
Jumbo Daniel

Posted on

10 2

How to use Tailwind CSS in an ExpressJS app.

This post will guide you if you want to use Tailwind CSS without having to use any bundler.

First use your terminal to change directory to your express application. Then install tailwind in your express app using:

npm i tailwindcss

Run this command to generate the tailwind configuration file. This will create a tailwind.config.js file in your root directory.

npx tailwindcss init

Create a tailwind.css file in your public folder, I would advice you to create a stylesheets folder under the public folder to properly seperate your files(public/stylesheets/tailwind.css). After that copy and paste the following code in the tailwind.css file.

@tailwind base;

@tailwind components;

@tailwind utilities; 
Enter fullscreen mode Exit fullscreen mode

Install postcss globally. This allows us to use postcss in other projects without reinstalling for each projects.

npm install -g postcss-cli

Create a postcss configuration file in your root directory and call it postcss.config.js. Then copy the following configuration into your postcss.config.js file:

module.exports = {
  plugins: [
    // ...
    require('tailwindcss'),
    require('autoprefixer'),
    // ...
  ]
}
Enter fullscreen mode Exit fullscreen mode

Add a build:css script to your package.json file so you can always build your css by running the command:

npm run build:css

"scripts": {
     "build:css": "postcss public/stylesheets/tailwind.css -o public/stylesheets/style.css"
  },
Enter fullscreen mode Exit fullscreen mode

Import style.css in your application as you normally would and run your app. Congratulations.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (3)

Collapse
 
sensi1707 profile image
Sensi1707

you just stopped the guide where i don't know how to do it...

Collapse
 
jumbo02 profile image
Jumbo Daniel

where step do you have a problem implementing?

Collapse
 
abid365 profile image
Syed Jawad Bin Azam

Is this a guide or a trauma ?

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay