Rails/Webpacker运行异常求助:React项目资产预编译报错
Yep, I’ve hit this exact set of errors before— it boils down to a version mismatch between Webpacker and your installed Webpack version. Let’s walk through the solutions step by step:
Why This Happens
The deprecation warnings about Tapable.apply and Tapable.plugin plus the TypeError: dep.getResourceIdentifier is not a function error are all signs that your Webpacker gem/npm package is expecting a different Webpack version than the 3.3.0 you’re using.
Webpacker has strict version compatibility:
- Webpacker 3.x works exclusively with Webpack 3.x
- Webpacker 4+ requires Webpack 4.x or higher
Chances are your project has a newer Webpacker version installed that’s trying to use Webpack 4’s new Tapable hooks API, which doesn’t exist in Webpack 3.3.0.
Solution 1: Downgrade Webpacker to Match Webpack 3.3.0
If you want to keep using Webpack 3.3.0, adjust your dependencies to use a Webpacker version compatible with it:
Update your Gemfile
Change thewebpackergem line to target the 3.x series:gem 'webpacker', '~> 3.6'Then run:
bundle installUpdate your npm dependencies
Open yourpackage.jsonand adjust the@rails/webpackerpackage to match the gem version (3.6.x):"@rails/webpacker": "~3.6.0"Then clean up old dependencies and reinstall:
rm -rf node_modules package-lock.json # or yarn.lock if using Yarn npm install # or yarn installVerify and test
Run the precompile command again to confirm the error is gone:rails assets:precompile RAILS_ENV=development
Solution 2: Upgrade Webpack to Match a Newer Webpacker (Optional)
If you’re open to upgrading Webpack to 4.x (which is more modern and supported), you can go this route instead:
Update Webpacker to 4.x
Modify your Gemfile:gem 'webpacker', '~> 4.0'Run
bundle install.Upgrade Webpack and related packages
Inpackage.json, update Webpack to 4.x and ensure@rails/webpackermatches the gem version:"webpack": "~4.46.0", "@rails/webpacker": "~4.0"Clean and reinstall dependencies as before, then run the precompile command.
Extra Checks
If you still run into issues, double-check for other Webpack plugins in your package.json (like html-webpack-plugin) that might be using versions incompatible with your Webpack version. For Webpack 3, use plugin versions that explicitly support Webpack 3 (e.g., html-webpack-plugin@3.x).
内容的提问来源于stack exchange,提问作者Snipper03

