通过 Heroku 部署 Rails 个应用程序使用了旧版本的 Node

Deploying Rails App via Heroku uses an old version of Node

在我的 Ubuntu 16.04 机器上尝试通过 Heroku“git push heroku master”部署我的 Rails 应用程序时,我收到以下错误:

[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

这是我的 package.json 文件(在根文件夹中):

{
  "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"
  }
}

节点--版本:

v13.10.1

heroku 运行 节点 --version:

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

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

您可以通过 package.json

在 Heroku 上指定节点版本
{
  "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"
  }
}

您可以在此处找到当前支持的节点版本:https://devcenter.heroku.com/articles/nodejs-support

不确定到底是什么问题,但问题与我使用的 Heroku buildpack (heroku-buildpack-bundler2) 有关

这是我解决问题的方法:

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

$ git push heroku master

现在一切正常:-)

Ruby 支持 - Installed binaries

默认情况下 Rails 具有 execjs gem 的应用程序将在应用程序中安装默认版本的 Node。默认安装的Node版本为:

10.15.3

如果您的 Ruby 应用程序需要特定版本的节点,您应该使用多个构建包首先安装节点然后安装 Ruby。

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

验证您的 buildpacks 是否设置正确并且 Node 位于 Ruby:

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

完成此操作后,您需要在应用的根目录中创建一个 package.json 文件。例如,要安装 8.9.4 版,您的 package.json 可能如下所示:

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

您可以在 package.json

的 engines.yarn 键中指定要使用的 Yarn 版本