简体   繁体   中英

Tailwind in React project - getting "Cannot find module 'autoprefixer'" error during setup

I'm following the documentation for setting up Tailwind in a React project over on https://tailwindcss.com/docs/guides/create-react-app . I've been following the steps, but when I get to the part where I'm supposed to run npx tailwindcss init in order to generate a tailwind.config.js file, I get the following error:

Cannot find module 'autoprefixer'
Require stack:
- C:\Users\[user]\AppData\Roaming\npm-cache\_npx\16096\node_modules\tailwindcss\lib\cli\commands\build.js
- C:\Users\[user]\AppData\Roaming\npm-cache\_npx\16096\node_modules\tailwindcss\lib\cli\commands\index.js
- C:\Users\[user]\AppData\Roaming\npm-cache\_npx\16096\node_modules\tailwindcss\lib\cli\main.js
- C:\Users\[user]\AppData\Roaming\npm-cache\_npx\16096\node_modules\tailwindcss\lib\cli.js

I've checked that I have autoprefixer in my node_modules folder and tried reinstalling it, but I get the same error. In my package.json , I have the following:

...
  "scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "react-scripts eject"
  },
...

as per the documentation. My craco.config.js file is as follows:

module.exports = {
  style: {
    postcss: {
      plugins: [
        require('tailwindcss'),
        require('autoprefixer'),
      ],
    },
  },
}

again, as per the documentation. I've also tried reinstalling the @craco/craco package to no avail, so at this point I'm stuck. Any help would be appreciated.

Can you try this:

npm uninstall tailwindcss postcss autoprefixer
npm install tailwindcss@latest postcss@latest autoprefixer@latest

npx tailwindcss init -p

npm uninstall tailwindcss postcss autoprefixer
npm install tailwindcss@npm:@tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9

here: https://github.com/tailwindlabs/tailwindcss/issues/2831

I have faced same problem.

at first I faced “Cannot find module 'autoprefixer'” error during setup.

than I tried "yarn" instead of "npm".

rest of process will be remain same.

it works now.

The issue is sometimes you install tailwind instead of tailwindcss

pnpm add tailwindcss

-- not --

pnpm add tailwind

This worked for me

npm install tailwindcss@latest postcss@latest autoprefixer@latest
npm install tailwindcss@npm:@tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9

This is simple way for Creating The Tailwind Configuration File

npm install tailwindcss@latest postcss@latest autoprefixer@latest
npx tailwindcss init

Just use:-

yarn add tailwindcss@npm:@tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9

instead of,

npm install tailwindcss@npm:@tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9

This worked for me.

I updated npm and nodejs and error is gone. Maybe it is a compatibility error.

npm update -g npm

{yourpackagemanager} upgrade nodejs -y

npm updated from " 6.11.3 " to " 6.14.17 ".

nodejs updated from " v12.10.0 " to " v18.9.1 ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM