Mac环境下$PATH导致Rails 5.1部署Heroku时Node版本异常问题
Hey Simon, let’s get this sorted out—your local setup looks totally fine, so the issue is definitely happening on Heroku’s end, not your machine’s PATH (all those entries in your local PATH don’t affect Heroku’s environment at all, so you can stop worrying about that!). Here’s what to do step by step:
1. Specify the Node.js version in your project
Heroku defaults to older Node versions unless you explicitly tell it which one to use. Since you’re running v8.9.4 locally, let’s lock that in:
- If you don’t already have a
package.jsonfile in your Rails root (Rails 5.1+ with Webpacker should have this, but double-check), create one. - Add an
enginessection to yourpackage.json(or update it if it exists):{ "name": "your-rails-app-name", "engines": { "node": ">=8.9.4" } } - Commit this change to Git:
git add package.json git commit -m "Specify Node.js version for Heroku"
2. Set the correct buildpack order
Heroku runs buildpacks in sequence, and if the Ruby buildpack runs before the Node.js one, it might pick up an outdated Node version. Let’s make sure Node.js comes first:
- Run these commands in your terminal:
heroku buildpacks:add --index 1 heroku/nodejs heroku buildpacks:add --index 2 heroku/ruby - Verify the order with:
You should seeheroku buildpacksheroku/nodejslisted first, thenheroku/ruby.
3. Purge Heroku’s build cache
Old cached build assets can sometimes cause version mismatches. Let’s clear them and redeploy:
- First, install the Heroku builds plugin if you don’t have it:
heroku plugins:install heroku-builds - Purge the cache:
heroku builds:cache:purge - Push a dummy commit to trigger a fresh build:
git commit --allow-empty -m "Purge Heroku build cache" git push heroku main
4. Verify the Node version on Heroku
Once deployment finishes, check that Heroku is using the correct Node version:
heroku run node -v
You should see v8.9.4 (or whatever version you specified) printed out. If that’s the case, your rake assets:precompile should run without the version error now.
内容的提问来源于Stack Exchange,提问作者Simon Cooper

