部署 [已解决] 在 Heroku 中部署 Rails,修改 Node 版本问题

codemonkey · September 22, 2017 · Last by codemonkey replied at September 30, 2017 · 1765 hits

我在 js 中使用了 es6 语法,例如 let 声明变量,但是默认 heroku 部署安装的是 node 的 v6,导致失败,于是我把 let 改成了 var 部署成功

问题来了,我想修改部署时安装 node 版本,我查了文档, https://devcenter.heroku.com/articles/ruby-support#installed-binaries

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

{ "engines" : { "node": "6.2.2" } }

Commit to git:

$ git add pacakage.json

$ git commit -m 'specify node version'

Now the next time you deploy your Ruby application will use Node version 6.2.2.

但是,我改成了如下

{
  "name": "shopa",
  "private": true,
  "dependencies": {},
  "engines": {
    "node": "8.5.0"
  }
}

部署的时候还是安装的 v6 不是我指定的 v8,我是免费的 heroku 用户,哪位朋友知道如何解决,我 google 了。没有找到解决办法,

--------------------------------分割线------------

解决办法就是要添加一个 nodejs 的 buildpack 操作如下:

1 如果你已经添加了 heroku/ruby,那么需要使用下面

heroku buildpacks:add --index 1 heroku/nodejs

2 然后在你的 package.json 里修改依赖版本

{
  "name": "shopa",
  "private": true,
  "dependencies": {},
  "engines": {
    "node": "8.5.0"
  }
}

所以在有了 heroku/ruby 时,使用 --index 1 添加 nodejs buildpack,确保在 ruby 构建前,部署 nodejs,

2 如果你你添加 heroku/nodejs 忘记使用 --index 1 可以使用 下面命令移除 heroku/ruby 一次,再添加

heroku buildpacks:remove heroku/ruby 

修改 buildpack

你是说 node v6 识别不了 let?

Reply to pynix

谢谢你的回答,我又看了一下文档,原来需要先添加一个 heroku/nodejs 这个 buildpack 才行,然后按照我以前操作在 package.json 里添加 node 需要的依赖版本就行,但是文档并没有明确提示这个关键点,我以为只要修改 package.json 就行

Reply to wind2729

恩,不识别 let,问题解决了

codemonkey closed this topic. 30 Sep 22:58
You need to Sign in before reply, if you don't have an account, please Sign up first.