19

I am initializing TailwindCSS using Create React App (CRA); so my project built with Vite, React, but can't get it to work.

It seems like postcss and autoprefixer is not getting installed, when I try to install manually it gives the following error:

warning Pattern ["postcss@^8.4.20"] is trying to unpack in the same destination "C:\\Users\\NUR\\AppData\\Local\\Yarn\\Cache\\v6\\npm-postcss-8.4.20-64c52f509644cecad8567e949f4081d98349dc56-integrity\\node_modules\\postcss" as pattern ["postcss@^8.4.18","postcss@^8.4.20"]. This could result in non-deterministic behavior, skipping.
[3/4] Linking dependencies...
warning " > [email protected]" has unmet peer dependency "postcss@^8.0.9".
9
  • Perhaps try use https://tailwindcss.com/docs/guides/vite as a reference instead of the guide for CRA. Commented Jan 2, 2023 at 21:16
  • 1
    @JohnLi I have tried using the vite guide , it still doesn't work and gives the same issue Commented Jan 2, 2023 at 21:18
  • Can you show you package.json and tailwind.config.cjs ? Commented Jan 2, 2023 at 21:36
  • 1
    I have copied the TailWind config exactly from the guide Commented Jan 2, 2023 at 21:43
  • I just tried and only one thing went wrong, there was no postcss.config.cjs Did you have it when you tried npx tailwindcss init -p ? Commented Jan 2, 2023 at 21:45

13 Answers 13

57

I followed the documentation to install tailwind with Vite and React and it didn't worked too. then i add these changes in vite.config.js and it worked.

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "tailwindcss";

// https://vitejs.dev/config/
export default defineConfig({
  css: {
    postcss: {
      plugins: [tailwindcss()],
    },
  },
});
Sign up to request clarification or add additional context in comments.

1 Comment

and if you want to keep your postcss.config.js, you can move tailwindcss() from the vite config file to your separate postcss config file - for me, I had to place it before the other plugins that I am using.
30

I followed the doc to install tailwind with Vite and React and it didn't work too.

Don't install packages manually, use the npx tailwindcss init -p but create manually the file postcss.config.cjs with :

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {}
  }
};

Don't forget the rest of the guide and it will work

1 Comment

In a comment, wrongly posted as an answer here, mentions "in changing the default export to a module export, at least in my case, breaks the entire app."
10

tailwindcss 3 react+vite issue

solved after the tailwind installation command

first

run : npm install -D tailwindcss@3 postcss autoprefixer

second

after that run the command : npx tailwindcss init -p that will create both tailwind.config.js and postcss.config.js

after that clear your index.css and add these

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

after that update the tailwind.config.js file to this

export default {
  content: ["./index.html", "./src/**/*.{html,js,ts,jsx,tsx}"],
  theme: {
    extend: {},
  },
  plugins: [],
};

Comments

7

This code working for me.

import { defineConfig } from "vite"; 
import react from "@vitejs/plugin-react"; 
import tailwindcss from "tailwindcss";

// https://vitejs.dev/config/ 
export default defineConfig({   
    plugins: [react()],   
    css: {
        postcss: {
            plugins: [tailwindcss()],
        },   
    }, 
});

2 Comments

Please explain how this addresses the issue. What have you changed? Code-only answers are not good answers
This vite.config.js file does the following: It integrates React support via @vitejs/plugin-react. It configures PostCSS to use Tailwind CSS for processing styles, ensuring the proper Tailwind classes are applied. It optimizes your development environment for React with fast refresh and efficient style processing.
3

Add this in tailwind.config.js

content: [
    "./index.html",
    "./src/**/*.{js,ts,jsx,tsx}",
  ],

Comments

2

I don't know if this helps anyone, but I had the same issue after following the guide, and all I had to do was add this line to my vite.config.js file.

import tailwindcss from "tailwindcss";

Comments

1

First confirm that TailwindCSS is set up correctly... you can use following commands to done this .

npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

After installing TailwindCSS create a new file postcss.config.js inside your project folder and copy and paste them.

export default {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
};

The Next Step is to create one more file inside your project folder called tailwind.config.js and copy and paste them.

export default {
  content: ["./src/**/*.{html,js,jsx}"],
  theme: {
    extend: {},
    },
  },
  plugins: [],
};

In the Final step, Locate the files inside your project folder called main.css or index.css copy and paste them.

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

I hope this will help.

Comments

1

It worked for me after adding <script src="https://cdn.tailwindcss.com"></script> in my index.html

Comments

0

I followed the doc to install tailwind with Vite and React and it didn't work too and I was also creating my postCSS config file manually too which was wrong.

All I needed to do was use the npx tailwindcss init -p and it created both my tailwind config file and postcss config file for me automatically and solved my problem

Comments

0

when you install react with vite@latest your autoprefixer node module is not install with node packages. so you need to generate first postcss.config.js using npm install -D autoprefixer and requred node module to run command npm install -D autoprefixer

in short :-

  1. need to install required postcss.config file : npm install -D autoprefixer
  2. install required node module : cmd npm install -D autoprefixer

1 Comment

respectfully, the language used in your answer is impossible to decipher.
0

Make sure that ./src/index.css file must contains:

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

It solved the issue for me. Hope it helps.

Comments

0

1 - Follow this documentation -> https://tailwindcss.com/docs/guides/create-react-app

2 - add this -> npm i postcss 3 - add this -> npm i autoprefixer 4 - create manually this file in your project -> postcss.config.js 5 - add below code inside of postcss.config.js

export default {
    plugins: {
      tailwindcss: {},
      autoprefixer: {},
    },
  }

Comments

-3

Remember to rerun your project after installing any package. After installing and configuring files tailwind did not work. When I stop and rerun my project it started working

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.