Prerequisites
Before starting the project make sure to install the following utilities:
Creating a new Express application
Let's create a fresh Express application so that we can go through all the steps together.
Step 1
Create new project.
mkdir my-project cd my-project npm init -y
Step 2
Install Express.
npm install express
Step 3
Install EJS. Depending on your requirements, you can choose other template engines. However, for this tutorial, we will be using EJS.
npm install ejs
Step 4
Create a folder named views
in the root
directory and add an index.ejs
file inside with a classic HTML structure.
Additionally, create a public
folder, then within it, create a css
folder and add an input.css
file - add your own css styles there. Attach a link to this file in the head
section.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link href="/css/input.css" rel="stylesheet" /> <title>TWE Express Integration tutorial</title> </head> <body> <main> <h1>Express application</h1> </main> </body> </html>
Step 5
In the root
directory create app.js
file where will be located your Express configuration.
const express = require("express"); const app = express(); const port = 3000; app.set("view engine", "ejs"); app.use(express.static("public")); app.get("/", (req, res) => { res.render("index"); }); app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
Step 6
To see if everything works, run the following command in the terminal:
node app.js
Installing and configuring Tailwind CSS and TW Elements
Step 1
Install Tailwind CSS.
npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p
File structure
If you have followed all instructions mentioned earlier, your file structure should look like this:
my-project/ ├── node_modules/ ├── public/ │ └── css/ | | └── input.css ├── views/ │ └── index.ejs ├── app.js ├── package-lock.json ├── package.json ├── postcss.config.js └── tailwind.config.js
Step 2
Add the paths to all of your template files in your tailwind.config.js
file.
/** @type {import('tailwindcss').Config} */ module.exports = { content: [ "./public/**/*.{js,css}", "./views/**/*.ejs", "./node_modules/tw-elements/js/**/*.js", ], darkMode: "class", plugins: [require("tw-elements/plugin.cjs")], };
Step 3
Replace your own styles in input.css
file with Tailwind directives.
@import url("https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,900&display=swap"); @tailwind base; @layer base { html { @apply text-surface; @apply bg-white; } html.dark { @apply text-neutral-50; @apply bg-body-dark; } } @tailwind components; @tailwind utilities; p { @apply leading-[1.6]; }
Step 4
Install TW Elements.
npm install tw-elements
Step 5
Update the app.js
file to enable serving static files from the node_modules
directory.
... app.use(express.static("public")); app.use(express.static("node_modules")); ...
Step 6
In the head
section of the index.ejs
file, substitute the href
attribute from input.css
to the Tailwind CSS file named output.css
, which will be generated during the Tailwind CLI build process. Furthermore, add a link to the TW Elements JS at the end of the body
tag.
... <head> ... <link href="/css/output.css" rel="stylesheet" /> ... </head> <body> ... <script src="/tw-elements/dist/js/tw-elements.umd.min.js" type="text/javascript"></script> </body> ...
Step 7
Add the TWE components you intend to use in your project.
... <main> <div class="mt-16 flex justify-center"> <p class="text-lg"> Hover the link to see the <a href="#" class="text-primary transition duration-150 ease-in-out hover:text-primary-600 focus:text-primary-600 active:text-primary-700 dark:text-primary-400 dark:hover:text-primary-500 dark:focus:text-primary-500 dark:active:text-primary-600" data-twe-toggle="tooltip" title="Hi! I'm tooltip" >tooltip</a > </p> </div> </main> ...
Step 8
Start the Tailwind CLI build process. Run the CLI tool to scan your template files for classes and build your CSS.
npx tailwindcss -i ./public/css/input.css -o ./public/css/output.css --watch
And run the local server. Awesome! You're all set to dive into using TW Elements for your Express project. Have fun!
node app.js