8

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 :(

Elton Serra
  • 497
  • 2
  • 5
  • 11
  • 3
    Execute `npm install postcss --save-dev`. It should resolve your issues. – gretro Apr 01 '18 at 04:39
  • I install postcss, but error continues. – Elton Serra Apr 01 '18 at 14:16
  • 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`. – pldg Apr 02 '18 at 01:56
  • 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. – Elton Serra Apr 03 '18 at 02:16
  • 1
    On Windows try to delete your *node_modules* && *package-lock.json* and then run `npm install` to re-install all packages. – pldg Apr 05 '18 at 22:55
  • I delete node_modules and package-lock.json, but still get the error. – Elton Serra Apr 06 '18 at 23:22

4 Answers4

20

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 postcss@8.1.0
Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
Joe Herbert
  • 442
  • 5
  • 13
2

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

Oleh Diachenko
  • 612
  • 1
  • 7
  • 9
2

Use the following steps

Delete package-lock.json

Delete node_modules

Run npm install

1

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

kmonsoor
  • 7,600
  • 7
  • 41
  • 55