TailwindCSS not working with Vite + React
Asked Answered
C

7

13

I am initializing TailWindCSS using https://tailwindcss.com/docs/guides/create-react-app on a Vite + React + JavaScript project but cant 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".
Caloric answered 2/1, 2023 at 20:58 Comment(6)
Perhaps try use https://tailwindcss.com/docs/guides/vite as a reference instead of the guide for CRA.Jannajannel
@JohnLi I have tried using the vite guide , it still doesn't work and gives the same issueCaloric
Can you show you package.json and tailwind.config.cjs ?Columbary
I have copied the TailWind config exactly from the guideCaloric
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 ?Columbary
No , same for me , postcss isn't getting installed, and the config is also not generated on npx initCaloric
C
23

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

Columbary answered 2/1, 2023 at 21:53 Comment(1)
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."Ultrastructure
P
38

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()],
    },
  },
});
Prosody answered 27/11, 2023 at 13:18 Comment(1)
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.Wotton
C
23

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

Columbary answered 2/1, 2023 at 21:53 Comment(1)
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."Ultrastructure
D
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

Doggy answered 9/9, 2023 at 15:52 Comment(0)
M
0

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";
Moment answered 24/5 at 20:58 Comment(0)
F
0

tailwindcss react+vite issue

solved after the tailwind installation command

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

and after that you need run the command : npm install -D autoprefixer

that will fixed the tailwindcss issue in react+vite

Flaviaflavian answered 26/8 at 9:33 Comment(0)
V
-1

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
Variation answered 7/1 at 3:17 Comment(1)
respectfully, the language used in your answer is impossible to decipher.Dameron
C
-2

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

Continuation answered 4/7 at 0:0 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Ickes

© 2022 - 2024 — McMap. All rights reserved.