Heroku部署React服务端渲染应用报错:找不到webpack-assets.json模块
Hey Jose, sorry to hear you're stuck with this Heroku deployment hiccup! Let's go through some practical fixes for the Cannot find module ../public/webpack-assets.json error you're facing with React-Cool-Starter:
1. Verify webpack-assets.json exists and is committed to Git
- First, run your local build command (like
npm run build) and check ifpublic/webpack-assets.jsonis actually generated. If it's missing locally, your build script might have an issue—double-check your Webpack or React-Cool-Starter config to ensure this file is output correctly. - Open your
.gitignorefile and make sure it isn't excludingwebpack-assets.jsonor the entirepublicdirectory. Heroku deploys from your Git repo, so if the file isn't committed, it won't be available on the server. Remove any unwanted ignore rules, commit the file, and redeploy.
2. Fix path resolution for webpack-assets.json
- Relative paths can behave differently on Heroku compared to your local environment. Try replacing the relative import with an absolute path using Node's
pathmodule. For example:const assets = require(path.resolve(__dirname, '../public/webpack-assets.json')); - Double-check your
webpack-isomorphic-toolsconfig file. Ensure theassetsfield points to the correct location ofwebpack-assets.json, and that any path-related settings align with where the file is generated during the build.
3. Ensure Heroku runs your build script
- Heroku might not automatically trigger your build process. Add a
heroku-postbuildscript to yourpackage.jsonto force the build during deployment:"scripts": { "build": "your-existing-build-command", "heroku-postbuild": "npm run build" } - Check your Heroku build logs (run
heroku logs --tail -a your-app-name) to confirm the build step runs successfully and generateswebpack-assets.json. Look for any errors during the build that might be preventing the file from being created.
4. Clear Heroku's cache and redeploy
- Old cached files can sometimes cause issues. Purge the cache and push a fresh deployment with these commands:
heroku plugins:install heroku-repo heroku repo:purge_cache -a your-app-name git commit --allow-empty -m "Purge Heroku cache" git push heroku main
内容的提问来源于stack exchange,提问作者Jose Ferran
相关产品推荐
相关产品推荐

