-->

Deploy rails react app with webpacker gem on AWS e

2020-06-16 05:07发布

问题:

I'm trying to deploy a rails 5.1 & react app created with webpacker gem using AWS Elastic Beanstalk. The problem is I keep getting the following error:

Webpacker requires Node.js >= 6.0.0 and you are using 4.6.0

I'm using Node 9.5.0 on my computer. Any suggestions??

回答1:

To install nodejs using yum (assuming you're using the default Amazon Linux)

https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora

curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
yum -y install nodejs

Now to execute this on your instances, you need to add the required commands to a config file inside the .ebextensions dir, something like: .ebextensions/01_install_dependencies.config

File contents:

commands:
  01_download_nodejs:
    command: curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
  02_install_nodejs:
    command: yum -y install nodejs


回答2:

For those that run into needing to also install Yarn, I have found the below just worked for me:

commands:
  01_install_yarn:
    command: "sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo && curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash - && sudo yum install yarn -y"
  02_download_nodejs:
    command: curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
  03_install_nodejs:
    command: yum -y install nodejs


回答3:

For those of you who found this issue when upgrading to Rails 6, I wrote a post on how to fix it.

Basically, you have to:

  • Create directory .ebextensions in the top level of your application
  • Create .config file which contains the commands to fix deployments (script here)
    • Add commands for upgrading nodejs to >=6.14.4
    • Add container commands to install webpack & precompile assets
  • Disable asset building through elastic beanstalk
    • set set RAILS_SKIP_ASSET_COMPILATION to True

What that amounts to is:

commands:

  01_install_yarn:
    command: "sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo && curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash - && sudo yum install yarn -y"
  02_download_nodejs:
    command: curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
  03_install_nodejs:
    command: yum -y install nodejs

container_commands:

  04_install_webpack:
    command: npm install --save-dev webpack
  05_precompile:
    command: bundle exec rake assets:precompile