rails: Rails 7 assets:precompile webpack not found

I have a Rails 6.1/Ruby 3.0.3 app where I ran bundle update rails, upgraded yarn versions, and ran rails app:update. This worked fine locally with no issues. I then pushed to production where one of the steps is ‘rails assets:precompile’ This command fails with:

Compiling...
Compilation failed:
yarn run v1.22.17
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

error Command "webpack" not found.

If I revert back to Rails version 6.1.4.4 the command works perfectly fine and compiles assets.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 8
  • Comments: 22 (1 by maintainers)

Commits related to this issue

Most upvoted comments

@bradbajuz was right and this PR, and specifically removing these lines, created this issue.

@chekkan solution worked for me, but the easy fix is to add again this line somewhere in your tasks (ex:lib/tasks/yarn.rake):

Rake::Task["assets:precompile"].enhance ["yarn:install"]

I had the same problem deploying rails website using digital ocean app platform after upgrading from version 6 to version 7. Digitalocean app platform uses the heroku buildpacks behind the scene.

Fixed for me by making sure webpacker version 5.4.3 was in Gemfile. Made sure to run bin/bundle update followed by bin/rails webpacker:install reviewing the changes carefully.

Created lib/tasks/before/assets_precompile.rake file.

task before_assets_precompile: :environment do
  # run a command which starts your packaging
  system('yarn')
end

# every time you execute 'rake assets:precompile'
# run 'before_assets_precompile' first
Rake::Task['assets:precompile'].enhance ['before_assets_precompile']

And finally set the environment variable RAILS_SERVE_STATIC_FILES to true in settings based on the assets pipeline documentation and config/environments/production.rb file.

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?

@bradbajuz, I had the same problem here. It seems to be related to yarn install not been called https://github.com/heroku/heroku-buildpack-ruby/issues/1240#issuecomment-996885689

Instead of add node buildpack I just update the webpacker gem to 6.0.0.rc.6 and it was working again. https://github.com/heroku/heroku-buildpack-ruby/issues/1240#issuecomment-996912552

Maybe upgrading webpacker solves docker as well.

If you’re using heroku, I resolved this by heroku buildpacks:add --index 1 heroku/nodejs

Here is the Dockerfile I am using to create the image:

FROM ruby:3.0.3

RUN apt-get update -yqq

RUN curl -sL https://deb.nodesource.com/setup_16.x  | bash - \
	&& apt-get install -y nodejs

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
	&& echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
	&& apt-get update -qq \
	&& apt-get install -y yarn

WORKDIR /usr/src/app
COPY Gemfile* /usr/src/app/
RUN gem update --system
RUN gem install bundler
RUN gem install openssl
RUN bundle install --jobs=4
RUN bundle package
RUN useradd --create-home -s /bin/bash -u 1000 1000
COPY . /usr/src/app/
RUN chown -R 1000:1000 /usr/src/app
USER 1000

I then deploy it to kubernetes where I run the image. The part that’s failing is one of my init containers that runs: RAILS_ENV=production rails assets:precompile

This container fails with the error message.

@MichaelHoste and @chekkan solution worked for me.

@cirdes , package.json and Gemfile.lock state 5.4.3.

I’ll update and report back.

Deployment works on Heroku once I removed yarn (using npm only) and placed the nodejs buildpack in front of the ruby buildpack.

I am using docker so that won’t work for me unfortunately. It’s just so strange that I can go back to 6 and everything works.