How To Add TailwindCSS to a New AdonisJS Project

This lesson covers creating a new AdonisJS project that includes Webpack Encore, then adding and configuring PostCSS and TailwindCSS within the project.

Jan 09, 22
4m 48s

Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.


Burlington, KY

If youโ€™re here, Iโ€™m going to assume you know what both AdonisJS and TailwindCSS are and at least the basics of them, so letโ€™s kick it!

Creating Our AdonisJS Project

First, weโ€™re going to want to create a new AdonisJS project. Iโ€™ll be naming mine tailwindcss-example, feel free to name yours whatever you wish.

npm init adonis-ts-app@latest tailwindcss-example

Once you enter this command itโ€™ll ask you a few questions so the project is generated to your liking. The only options we care about are the project structure and Webpack Encore. Weโ€™ll want to use the โ€œwebโ€ project structure and youโ€™ll want to select โ€œyesโ€ when asked if Webpack Encore should be configured. For everything else, please choose to your liking.

โฏ Select the project structure ยท web
โฏ Enter the project name ยท tailwindcss-example
โฏ Setup eslint? (y/N) ยท yes/no, your preference
โฏ Setup prettier? (y/N) ยท yes/no, your preference
โฏ Configure webpack encore for compiling frontend assets? (y/N) ยท yes

Once this has been completed successfully youโ€™ll see something similar to the below in your terminal.

โฏ Scaffold project 40 ms
โฏ Install dependencies 37 s
โฏ Configure installed packages 1.22 s
โฏ Configure webpack encore 14 s

[ success ]  Project created successfully

โ”‚    Run following commands to get started             โ”‚
โ”‚                                                      โ”‚
โ”‚    โฏ cd tailwindcss-example                          โ”‚
โ”‚    โฏ node ace serve --watch                          โ”‚
โ”‚                                                      โ”‚

Now that our project is created, lets change into itโ€™s directory.

cd tailwindcss-example

Installing Required Dependencies

Once youโ€™ve changed into your project directory, itโ€™s time to install the required dependencies! First, since TailwindCSS uses PostCSS, weโ€™ll want to install postcss-loader for Webpack so we can compile our TailwindCSS.

npm i -D postcss-loader

Then, weโ€™ll need to also install TailwindCSS.

npm i -D tailwindcss

Weโ€™ll also want to have TailwindCSS generate its configuration file, which we can do by running the below.

npx tailwindcss init

This will generate the tailwind.config.js file within the root of our project.

Configuring PostCSS

Once we have our dependencies installed, itโ€™s time to jump inside our projectโ€™s code. Letโ€™s first jump into our webpack.config.js file at the root of our project. Go ahead and scroll down to about line 172 and uncomment Encore.enablePostCssLoader(). This will enable the postcss-loader we just installed within our Webpack configuration.

// webpack.config.js

// Encore.enablePostCssLoader() // ๐Ÿ‘ˆ uncomment this

Encore.enablePostCssLoader() // ๐Ÿ‘ˆ so it looks like this

Next, in order for PostCSS to know what plugins to use for our project, weโ€™ll need to define a PostCSS configuration within our project. There are a few ways to go about this, today letโ€™s just go with the default way of adding a postcss.config.js file to the root of our project. Then, within this file, weโ€™ll want to define TailwindCSS as a plugin, like the below.

// postcss.config.js

module.exports = {
  plugins: [

Thatโ€™s all we need to do to get PostCSS working within our project. Next, weโ€™ll move on to TailwindCSS.

Configuring TailwindCSS

By default, when we start our server up TailwindCSS is going to to purge all the TailwindCSS classes not in-use within our project. So, weโ€™ll want to define the content files it should check for class usages within our project. We can do this by defining template paths within our tailwind.config.js fileโ€™s content array, like the below.

// tailwind.config.js

module.exports = {
  content: [
    './resources/**/*.{edge,js,ts,vue,jsx,tsx}' // ๐Ÿ‘ˆ
  theme: {
    extend: {},
  plugins: [],

This will include all files within the resources directory ending with the extension edge, js, ts, vue, jsx, and tsx. Feel free to alter what I have here for your use case.

Lastly, weโ€™ll want to add the TailwindCSS directives to our CSS file.

/* resources/css/app.css */

@tailwind base;
@tailwind components;
@tailwind utilities;

AdonisJS starts us out with some boilerplate styles for the welcome page. Feel free to replace all of that boilerplate in your CSS file with the above.

Testing It Out

With that, TailwindCSS should now be fully set up within our project, congratulations! Next, letโ€™s go ahead and boot our server up and test it out.

npm run dev

Once you run that you should see something like the below.

npm run dev                                                                                                                                                                   

# โฏ [email protected] dev
# โฏ node ace serve --watch

# [ info ]  building project...
# [ info ]  starting http server...
# [1641664665768] INFO (tailwindcss-example/30436 on toms-mbp.lan): started server on
# [ encore ] Running webpack-dev-server ...
# [ info ]  watching file system for changes
# โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
# โ”‚                                                             โ”‚
# โ”‚    Server address: <>                  โ”‚
# โ”‚    Watching filesystem for changes: YES                     โ”‚
# โ”‚    Encore server address: <http://localhost:8080>           โ”‚
# โ”‚                                                             โ”‚
# โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
# [ encore ]  DONE  Compiled successfully in 586ms12:57:46 PM
# [ encore ] webpack compiled successfully
# UPDATE: public/assets/manifest.json
# UPDATE: public/assets/entrypoints.json

This is stating that our AdonisJS server has booted up successfully at http://localhost:3333. Itโ€™s also stating that our Webpack Encore server, which will serve our style and script assets, has successfully started at http://localhost:8080.

Now, the โ€œwebโ€ project structure for AdonisJS conveniently comes out-of-the-box with our CSS and JavaScript file rigged up to our welcome page. So, we donโ€™t need to do anything to get those working. We can jump straight into our page at http://localhost:3333.

Note this only happens when you also install Webpack Encore at the time your project is created.

You should see a bland page where all the test is in the top-right corner and all the text is the same size, despite the first line being an H1. If thatโ€™s what you see all is working well. All the text is the same size due to TailwindCSSโ€™ CSS reset.

Testing A TailwindCSS Class

Just to be sure letโ€™s do the infamous CSS test of making the text red. Go ahead and jump into the welcome pageโ€™s code at resources/views/welcome.edge and on the H1 element add the TailwindCSS class text-red-600.

{{-- resources/views/welcome.edge --}}

{{-- ... --}}

    {{-- ๐Ÿ‘‡ let's add the class 'text-red-600' to the h1 --}}
    <h1 class="title text-red-600"> It Works! </h1>
    <p class="subtitle">
      Congratulations, you have just created your first AdonisJS app.
    {{-- ... --}}

Once you save this change, the Webpack Encore will recognize the change and immediately reflect the change in your browser. So, go ahead and open your browser and you should now see the heading is a nice red color.

If thatโ€™s what you see then all is working well!

You can find the code for this lesson at our adonisjs-tailwindcss-example repository on GitHub.

Join The Discussion! (5 Comments)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 2 years ago

    Love the in depth info. Cheers!


    Please sign in or sign up for free to reply

    1. Commented 2 years ago

      Many thanks, figgyzox!!


      Please sign in or sign up for free to reply

  2. Anonymous (BarnacleBonny419)
    Commented 2 years ago

    AH! Finally! Finally someone told me how to do thisโ€ฆ Thanks alot!


    Please sign in or sign up for free to reply

  3. Anonymous (LimpetKerri724)
    Commented 1 year ago

    AH! Finally! Finally someone told me how to do thisโ€ฆ Thanks alot!


    Please sign in or sign up for free to reply

    1. Commented 1 year ago

      Anytime! :)


      Please sign in or sign up for free to reply