You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac环境下$PATH导致Rails 5.1部署Heroku时Node版本异常问题

Fixing Node.js Version Mismatch for Rails 5.1 on Heroku

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.json file in your Rails root (Rails 5.1+ with Webpacker should have this, but double-check), create one.
  • Add an engines section to your package.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:
    heroku buildpacks
    
    You should see heroku/nodejs listed first, then heroku/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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:42:47