Deploying Rails App via Heroku uses an old version of Node

Viewed 1070

While trying to deploy my Rails app via Heroku "git push heroku master" on my Ubuntu 16.04 machine, I receive the following error:

[2/4] Fetching packages...
remote:          error @rails/webpacker@4.2.2: The engine "node" is incompatible with this module. Expected version ">=8.16.0".
remote:        error An unexpected error occurred: "Found incompatible module".
remote:        info If you think this is a bug, please open a bug report with the information provided in "/tmp/build_c472d4366cfa53133fe29a2426a45a7b/yarn-error.log".
remote:        info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
remote:
remote:  !
remote:  !     Precompiling assets failed.
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote:
remote:  !     Push failed

This is my package.json file (in root folder):

{
  "name": "site",
  "private": true,
  "dependencies": {
    "@rails/webpacker": "4.2.2",
    "flatpickr": "^4.6.3",
    "jquery": "^3.3.1",
    "node": "^12.x",
    "node-sass": "^4.13.1",
    "reading-time": "^1.2.0",
    "simple-lightbox": "^2.1.0"
  },
  "devDependencies": {
    "@babel/plugin-syntax-dynamic-import": "^7.8.3",
    "webpack-cli": "^3.3.11",
    "webpack-dev-server": "^3.10.3"
  }
}

node --version:

v13.10.1

heroku run node --version:

Running node --version on ⬢ -site... up, run.4229 (Hobby)
v12.16.1
3 Answers

https://devcenter.heroku.com/articles/deploying-nodejs

You can specify a node version on Heroku via your package.json

{
  "name": "node-example",
  "version": "1.0.0",
  "description": "This example is so cool.",
  "main": "web.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "example",
    "heroku"
  ],
  "author": "jane-doe",
  "license": "MIT",
  "dependencies": {
    "express": "^4.9.8"
  },
  "engines": {
    "node": "10.x"
  }
}

You can find the currently supported node versions here: https://devcenter.heroku.com/articles/nodejs-support

Not sure exactly what it is, but the issue had something to do with the Heroku buildpack I was using (heroku-buildpack-bundler2)

Here's what I did to solve it:

$ heroku buildpacks:set heroku/ruby
$ heroku buildpacks:add --index 1 heroku/nodejs

$ git push heroku master

All works fine now :-)

Ruby Support - Installed binaries

By default Rails applications that have the execjs gem will have a default version of Node installed in the application. The version of Node installed by default is:

10.15.3

If you need a specific version of node for your Ruby application you should use multiple buildpacks to first install node then install Ruby.

$ heroku buildpacks:add heroku/nodejs
$ heroku buildpacks:add heroku/ruby

Verify that your buildpacks are set correctly and that Node comes before Ruby:

$ heroku buildpacks
=== myapp Buildpack URLs
1. heroku/nodejs
2. heroku/ruby

Once you have done this you’ll need a package.json file in the root of your app. For example to install version 8.9.4 your package.json could look like this:

{ "engines" : { "node": "8.9.4" } }

You can specify the version of Yarn you want to use in the engines.yarn key in your package.json

Related