Heroku部署create-react-app应用时无法解析react-dom模块求助
react-dom Module Despite It Being in client/package.json Hey there, let's work through this tricky Heroku build issue together. First, let's zero in on a key warning from your log that gives us a big clue:
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
This tells us Heroku isn't installing dependencies in your client directory during the deployment process—even though react-dom is listed in your client's package.json, the module can't be found because it was never installed. Here are actionable fixes to try:
1. Update Your Root package.json Build Scripts
Right now, your root build script only runs cd client && npm run build—it skips the critical step of installing the client's dependencies. Modify your root package.json to include dependency installation before building:
// Root package.json "scripts": { "start": "node server.js", // Adjust this to match your Express entry file "build": "cd client && npm install && npm run build", "heroku-postbuild": "cd client && npm install && npm run build" }
The heroku-postbuild script is tailored for Heroku's deployment pipeline, so adding it ensures the client's dependencies get installed reliably, even if the default build script behaves unexpectedly.
2. Align Engine Versions Across Root and Client
Make sure your root package.json uses the same engines configuration as your client to avoid version mismatches that can break dependency installation:
// Root package.json "engines": { "npm": "5.6.0", "node": "8.11.1" }
Heroku uses these specified versions to run npm and Node.js, so matching them across both directories prevents compatibility issues during dependency installs.
3. Double-Check Your .gitignore
While it's normal to ignore node_modules, confirm your .gitignore isn't accidentally excluding critical files in the client directory (like package.json). A missing package.json would prevent Heroku from knowing what dependencies to install.
4. Force Cache Clean (Last Resort)
If dependency installation is failing due to corrupted cached packages, add a cache-clearing step to your build script:
"build": "cd client && npm cache clean --force && npm install && npm run build"
This wipes any problematic cached data before installing fresh dependencies.
After making these changes, commit your updates and push to Heroku again. This should ensure react-dom and all other client dependencies are properly installed before the build runs.
内容的提问来源于stack exchange,提问作者Alex Hayden

