Webpacker: Capistrano deploy fails on compiling webpacker assets

Created on 24 Nov 2017  ·  14Comments  ·  Source: rails/webpacker

Just used webpacker gem to compile assets in development and it works like a charm. But the problem is I cannot deploy my project (I'm using EC2 instance). Here's the deploy log:

00:48 deploy:assets:precompile
01 ~/.rvm/bin/rvm default do bundle exec rake assets:precompile
01 Webpacker is installed 🎉 🍰
01 Using /home/ubuntu/bravado/releases/20171124194740/config/webpacker.yml file for setting up webpack paths
01 Compiling…
01 Compilation failed:

bundler: failed to load command: webpack (/home/ubuntu/bravado/shared/bundle/ruby/2.4.0/bin/webpack)



Errno::ENOENT: No such file or directory - /home/ubuntu/bravado/releases/20171124194740/node_modules/.bin/webpack

/home/ubuntu/bravado/shared/bundle/ruby/2.4.0/bundler/gems/webpacker-386fd850a315/lib/webpacker/webpack_runner.rb:11:in `exec'

/home/ubuntu/bravado/shared/bundle/ruby/2.4.0/bundler/gems/webpacker-386fd850a315/lib/webpacker/webpack_runner.rb:11:in `block in run'

/home/ubuntu/bravado/shared/bundle/ruby/2.4.0/bundler/gems/webpacker-386fd850a315/lib/webpacker/webpack_runner.rb:10:in `chdir'

/home/ubuntu/bravado/shared/bundle/ruby/2.4.0/bundler/gems/webpacker-386fd850a315/lib/webpacker/webpack_runner.rb:10:in `run'

/home/ubuntu/bravado/shared/bundle/ruby/2.4.0/bundler/gems/webpacker-386fd850a315/lib/webpacker/runner.rb:6:in `run'

/home/ubuntu/bravado/shared/bundle/ruby/2.4.0/bundler/gems/webpacker-386fd850a315/exe/webpack:8:in `<top (required)>'

/home/ubuntu/bravado/shared/bundle/ruby/2.4.0/bin/webpack:22:in `load'

/home/ubuntu/bravado/shared/bundle/ruby/2.4.0/bin/webpack:22:in `<top (required)>'

Most helpful comment

@gauravtiwari @sdellis I believe it should be described better, but the solution is to add the following to your config/deploy.rb:

before "deploy:assets:precompile", "deploy:yarn_install"

namespace :deploy do
  desc 'Run rake yarn:install'
  task :yarn_install do
    on roles(:web) do
      within release_path do
        execute("cd #{release_path} && yarn install")
      end
    end
  end
end

All 14 comments

Please add yarn install before assets:compile in your deployment task.

I'm getting the same error. Yarn is already installed globally on my server.

I don't seem to have assets:compile as a Capistrano deployment task. I'm a javascript dev that's pretty new to Capistrano, so there may be something I'm missing.

@sdellis are you using https://github.com/capistrano/rails ?

Yarn is already installed globally on my server.

@sdellis not this but run yarn install to install npm dependencies on your project.

@gauravtiwari @sdellis I believe it should be described better, but the solution is to add the following to your config/deploy.rb:

before "deploy:assets:precompile", "deploy:yarn_install"

namespace :deploy do
  desc 'Run rake yarn:install'
  task :yarn_install do
    on roles(:web) do
      within release_path do
        execute("cd #{release_path} && yarn install")
      end
    end
  end
end

@allaud thanks for sharing 👍 💯 , that's what I meant but wasn't sure what tool is being used to deploy.

Would be nice if you could please make a PR to add this to deployment docs under Capistrano :)

@allaud @gauravtiwari I think yarn install is executed before assets:precompile automatically if you are using Rails 5.1, isn't the case in your app?

I migrated successfully an app from Sprockets to Webpacker without doing any changes in the Capistrano deployment files (just installed Yarn in production servers)

Edit: Confirm, this should be handled by https://github.com/rails/rails/blob/5ccdd0bb6d1262a670645ddf3a9e334be4545dac/railties/lib/rails/tasks/yarn.rake.

I think the purpose of enhancing the assets:precompile task with yarn:install and webpacker:compile was to allow a seamless migration from Sprockets to Webpacker, this should work™

@guilleiguaran Yes, that's true actually. Even it covers rails < 5.1

https://github.com/rails/webpacker/blob/master/lib/tasks/webpacker/compile.rake#L13

But I felt these folks might be clearing out enhancements from assets:precompile for some reason (there was issue on this too).

Ah, I see, I think I've experienced issues with the enhancements and found that the problem were related to the order of sprockets-rails and webpacker gems in the Gemfile as described in https://github.com/rails/webpacker/pull/985

I'll try to reproduce the problem again and if isn't fixed yet I'll merge #985

The example code @allaud posted worked for me. Thanks!

@guilleiguaran 👍

@sdellis @allaud As @guilleiguaran pointed out this is automatic though so that code is unnecessary but seems like a change broke this. Are you using master or 3.0.2?

@gauravtiwari I'm using Rails 5.1 and gem 'webpacker', '~> 3.0'. Should I be using master?

I also don't want to use yarn, but npm, as yarn gives me errors with a typescript "typings" dependency (required by another npm dependency). I have no such issue with npm. Is there a way to configure this to use npm and not yarn?

@sdellis Right, you might wanna upgrade to 3.0.2 - bundle update webpacker

Yeah, you can do that and for same you would need to configure npm - remove yarn.lock file and use npm to install node deps. The same needs to be done in capistrano task as well.

before "deploy:assets:precompile", "deploy:npm_install"

namespace :deploy do
  desc 'Run rake npm install'
  task :npm_install do
    on roles(:web) do
      within release_path do
        execute("cd #{release_path} && npm install")
      end
    end
  end
end

Please note we officially support yarn but don't think it's a problem to use npm if you want to.

Thank you, @gauravtiwari ! That all works for me.

Was this page helpful?
0 / 5 - 0 ratings