Webpack: Module build failed: Error: Cannot find module 'postcss'
Asked Answered
M

4

9

I am trying to integrate my webpack project with sass-loader, but I am get this error:

 Module build failed: Error: Cannot find module 'postcss'

I already installed postcss and postcss-loader with command:

 npm install postcss postcss-loader --save-dev

After install, I continue to get the same error message. I test integrate my project only with css-loader and the same error occurs. I think postcss is a dependency of the css-loader.

In my webpack.config.js I use this configuration in my module.rules:

 {
    test: /\.scss$/,
    use: [{
          loader: "style-loader" // creates style nodes from JS strings
      }, {
          loader: "css-loader" // translates CSS into CommonJS
      }, {
          loader: "sass-loader" // compiles Sass to CSS
      }
    ]
  }

I installed all this loaders and node-sass, I don't know what I am doing wrong :(

Marcelinomarcell answered 1/4, 2018 at 4:31 Comment(6)
Execute npm install postcss --save-dev. It should resolve your issues.Copybook
I install postcss, but error continues.Marcelinomarcell
Seems strange to me, sass-loader only require node-sass to be installed as peerDependency. You don't need postcss to use sass-loader. Anyway, try to install both postcss and postcss-loader with npm install --save-dev postcss postcss-loader, and try to add {loader: "postcss-loader"} in between your sass-loader and css-loader.Magi
I try to execute my project in a linux machine and I don't have any problems. For some reason in my windows machine I always get this error. I verify in my node_modules/postcss only have these files: node_modules and package.info. It is like modules not installed correctly.Marcelinomarcell
On Windows try to delete your node_modules && package-lock.json and then run npm install to re-install all packages.Magi
I delete node_modules and package-lock.json, but still get the error.Marcelinomarcell
C
22

I had this error when using autoprefixer with postcss through postcss-cli. I solved it by rolling postcss back to version 8.1.0 (current version is 8.1.1 at time of writing). This can be easily done by running the below command in your terminal.

npm i [email protected]
Cathee answered 13/10, 2020 at 9:50 Comment(0)
A
3

In my case problem was solved by delete package.lock.

Ataghan answered 28/3, 2022 at 12:31 Comment(1)
This can solve our problem. this should be correct answer.Kezer
L
3

Use the following steps

Delete package-lock.json

Delete node_modules

Run npm install

Landel answered 3/1, 2023 at 16:48 Comment(0)
A
1

Upgrading postcss-cli to the new version 9.0.1 fixed it for me.

Actinomycin answered 9/11, 2021 at 20:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.