create-react-app: postcss plugins not found when compiling with CRA v5.0.0 and fomantic-ui
Describe the bug
CRA v5 appears to be incompatible with fomantic-ui, since installing this package causes compile errors about postcss plugins not existing
Did you try recovering your dependencies?
Yes
❯ npm --version 8.5.4
Which terms did you search for in User Guide?
I searched for postcss
Environment
Environment Info:
current version of create-react-app: 5.0.0 running from /home/davidg/.npm/_npx/c67e74de0542c87c/node_modules/create-react-app
System: OS: Linux 5.10 Ubuntu 18.04.6 LTS (Bionic Beaver) CPU: (8) x64 Intel® Core™ i7-6700 CPU @ 3.40GHz Binaries: Node: 16.8.0 - ~/.nvm/versions/node/v16.8.0/bin/node Yarn: Not Found npm: 8.5.4 - ~/.nvm/versions/node/v16.8.0/bin/npm Browsers: Chrome: 99.0.4844.51 Firefox: Not Found npmPackages: react: ^17.0.2 => 17.0.2 react-dom: ^17.0.2 => 17.0.2 react-scripts: 5.0.0 => 5.0.0 npmGlobalPackages: create-react-app: Not Found
Steps to reproduce
npx create-react-app@latest cra-test --use-npm
Follow documented fomantic-ui install instructions:
2. npm install --ignore-scripts fomantic-ui
3. cd node_modules/fomantic-ui
4. npx gulp install
(select defaults)
5. npx gulp build
7. npm start
(this works normally)
8. rm node_modules package-lock.json -Rf
9. npm install
10. npm start
Expected behavior
Successful compilation
Actual behavior
ERROR in ./src/index.css (./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[5].use[1]!./node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[5].use[2]!./node_modules/source-map-loader/dist/cjs.js!./src/index.css) Module Error (from ./node_modules/postcss-loader/dist/cjs.js): Loading PostCSS “postcss-flexbugs-fixes” plugin failed: Cannot find module ‘postcss-flexbugs-fixes’ Require stack:
/home/davidg/cra-test/node_modules/postcss-loader/dist/utils.js /home/davidg/cra-test/node_modules/postcss-loader/dist/index.js /home/davidg/cra-test/node_modules/postcss-loader/dist/cjs.js /home/davidg/cra-test/node_modules/loader-runner/lib/loadLoader.js /home/davidg/cra-test/node_modules/loader-runner/lib/LoaderRunner.js /home/davidg/cra-test/node_modules/webpack/lib/NormalModule.js /home/davidg/cra-test/node_modules/webpack-manifest-plugin/dist/index.js /home/davidg/cra-test/node_modules/react-scripts/config/webpack.config.js /home/davidg/cra-test/node_modules/react-scripts/scripts/start.js
Reproducible demo
I hope the steps above are concise enough to serve as a reproducible demo also
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 15
- Comments: 18
Additional notes:
Workaround:
npm install postcss-flexbugs-fixes postcss-normalize postcss-preset-env
(even though according tonpm ls
these are all already installed as dependencies of react-scriptsI have the same issue except I’m not using fomantic-ui but Material-UI. The
npm install postcss-flexbugs-fixes postcss-normalize postcss-preset-env
workaround also fixes the issue for me.I believe something in my codebase is triggering the postcss which is failing since the packages are installed under
node_modules/react-scripts/node_modules/
Before running
npm install postcss-flexbugs-fixes
After running
npm install postcss-flexbugs-fixes
I’m pretty sure the above change in postcss-flexbugs-fixes package location is the reason why it starts working. Do you know if there’s a way to fix it other than installing those three packages? Also any idea what is triggering the postcss processing in cra application? I tried but couldn’t find the culprit from my fairly large codebase.
Rebuilding the lockfile worked for me as well.
For those still struggling with this problem. I use craco to customize the webpack config and remove postcss.
I believe @samuliasmala is correct that the relative positioning between packages is the fundamental issue.
Solution 1
I was able to solve the issue by manually relocating the
postcss-loader
package in mypackage-lock.json
file.Git diff package-lock.json
In short, I changed the
node_modules/postcss-loader
key under thepackages
section ofpackage-lock.json
tonode_modules/react-scripts/node_modules/postcss-loader
and relocated that entry so thepackages
section remained in tidy alphabetical order. After this, runningnpm i
will make a few other small modifications to the file (for me it removed thenode_modules/postcss-loader/node_modules/semver
key frompackages
and removed thepostcss-loader
entry from thedependencies
section, nesting it underdependencies['react-scripts'].dependencies
instead.From here,
npm run build
succeeds just fine. I have no idea whypostcss-loader
was not already located underreact-scripts
, as that’s the only package that depends on it. Perhaps I previously had another package installed that depended on it? Unsure.Solution 2
I wasn’t entirely comfortable with manually editing this generated file (though after it’s committed to version control there it shouldn’t be an issue), so I also messed around with rebuilding the
package-lock.json
file altogether. This approach worked too, though it was a little more complicated and involved updating thousands of sub-dependencies. My project depends on two packages which themselves depend onpostcss
:react-scripts
andmadge
, which I use in a script to detect cyclical imports in my codebase:Because both
react-scripts
andmadge
depend onpostcss
, thepostcss
package is hoisted up the tree to their common ancestor package, which is the repository root. If I uninstallmadge
, this alone does not solve the problem. However, if I delete thepackage-lock.json
file, delete mynode_modules
directory and rerunnpm install
, things are different:In other words, forcing npm to rebuild the
package-lock.json
file solved the issue. What makes this extra frustrating is that, after doing all this, I can reinstall the exact same version ofmadge
and my build will continue to run just fine:Deleting the
node_modules
directory now and runningnpm i
again (using the newly generated lockfile) does not break the build. At this point, mypackage.json
file has not changed at all, only thepackage-lock.json
file which has thousands of lines changed so I can’t possibly determine which of those changes resolved the problem. However, following the re-generation of the lockfile:At the end of the day, I have no idea why rebuilding the lockfile caused these packages to be hoisted to the repository root, but I strongly suspect that’s why the build now succeeds.
Facing this issue now. My team does use craco, but what does this actually do? I’m hesitant to rebuild the lockfile since that will imply many dependency changes.