Webpacker: asset_compile fails during yarn install

Created on 14 Sep 2017  ·  16Comments  ·  Source: rails/webpacker

We are using beanstalk as our server. When deploying the logs, we get the following

```yarn install v1.0.2
[1/4] Resolving packages...
Webpacker is installed 🎉 🍰
Using /var/app/ondeck/config/webpacker.yml file for setting up webpack paths
Compiling…
Compilation failed:

./bin/webpack:26:in exec': No such file or directory - /var/app/ondeck/node_modules/.bin/webpack (Errno::ENOENT) from ./bin/webpack:26:inblock in

'
from ./bin/webpack:25:in chdir' from ./bin/webpack:25:in
'
(Executor::NonZeroExitStatus)
```
It appears there is an error installing yarn packages as it is not going to step 2 or 4.

When I ssh into the server, go to the ondeck directory, which has the newest code I believe and then I run yarn install, I get:

error Could not write file "/var/app/ondeck/yarn-error.log": "EACCES: permission denied, open '/var/app/ondeck/yarn-error.log'" error An unexpected error occurred: "EACCES: permission denied, mkdir '/var/app/ondeck/node_modules'".

when I use sudo yarn install it works and when I update the permissions with sudo chmod -R 777 /var/app, it works. So I tried and add the chmod command to a .ebextensions config file but I am having no luck.

Most helpful comment

Hi, not sure if this is related but I ran into a similar issue (same logs "No such file or directory node_modules/.bin/webpack (Errno::ENOENT)") on a project that I updated from Rails 4.2 to Rails 5.2.

After digging, here is what was happening:

After adding the bin/yarn file (copied from a Rails 5 project), it worked!

All 16 comments

@mlennie When you logged onto your server were you logging in with the deployment user account? I am not familiar with beanstalk but I did want to point out that even if sudo chmod -R 777 /var/app works for you, you are granting public permission to modify anything in those directories which should not be necessary and might be harmful/dangerous. I would suggest investigating again ensuring that you are using the exact deployment user and then minimizing your chmod commands to only make changes to the exact directories that are affected, and then only make changes that enable the deployment user to have the proper permissions. You may need to change group permissions on the parent directory or ensure that your deployment user is a member of the same group as what your app runs under.

I'm wondering if everything else in your app worked properly before adding webpack or is this a new app? I guess the real question is, was everything deploying properly before? If not, then I would also consider reaching out to your hosting provider or anyone who might already be helping you support your servers as well. It seems like this is mostly a server setup issue and likely does not have anything specific to do with the webpacker gem or webpack.

It seems strange to me that you are running into this issue and feels like a server setup or general config related thing. Let me know if any of that helps or of your progress and I'll do my best to offer any help that I can. Thanks.

@mlennie You need to make sure ./bin/webpack binstub is checked into your source control. This file is generate when you run bundle exec rails webpacker:install

I have got same issue. Added Webpacker gem to existing app, run webpacker:install and all works well. BUT if I check source to GIT (note that node_modules are ignored) and then clone it to other folder (to test fresh install), run bundle. Webpacker is already installed (using Rbenv). When I then run bundle exec rspec, it will fail with above error on webpacker compilation.
This is because it expects that I have /node_modules/.bin/webpack. After manually invoke webpacker:yarn_install all went ok.

My app is running on rails 5.1.3 and in Webpacker rake tasks is

unless Rake::Task.task_defined?("yarn:install")
  # For Rails < 5.1
   Rake::Task["webpacker:yarn_install"].invoke
end

Maybe there is some hook in Rails 5.1 but it is not working in my app (continuously upgraded from Rails 4).

I got yarn:install between rake tasks, but I do not know when it should be executed (automagically?)

@foton You would need to run yarn or yarn install like bundle after cloning since node_modules are ignored. No need to run webpacker:yarn_install, it's same thing.

yarn:install will be executed when you will run assets:precompile - https://github.com/rails/webpacker/blob/master/lib/tasks/webpacker/compile.rake#L27

@gauravtiwari not exactly. It willbe run only if there is no yarn:install task. But this task is commited in repository.
I think, that simple solution would be to check if there is node_modules folder and if it is not than run yarn:check and yarn:install.

Or at least raise exception if Webpacker could not find /node_modules/.bin/webpack. It is logged in test.log, but rising it to stderr would be IMHO better.

I had a similar issue today while deploying Rails 5.1.4 via Capistrano. Was able to resolve it by comment out /node_modules in .gitignore file :)

For what it's worth, I also had the same problem deploying Rails (5.1.4) because of npm modules not being installed during assets:precompile. I manually executed yarn:install and it literally did nothing. Then I executed webpacker:yarn_install and after it installed all packages assets:precompile went through without any errors. I decided to manually install npm packages during deployment before assets:precompile.

UPD: Okay, I got it. There's no yarn executable in ./bin folder, so that's why it didn't do anything. Solved it by putting the following temporary fix to ./bin folder:

#!/bin/sh -e

yarn "$@"

I solved it as described in https://stackoverflow.com/questions/41657226/customize-aws-elasticbeanstalk-nodejs-install-use-yarn, install yarn just before the compilation of the assets.

I don't think this issue should be closed, this looks like a real bug in either Webpacker or Rails. Here's the details.

We're having the same problem caused by the asset precompile looking for node_modules/.bin/webpack during deployment (note that it's not looking for the rails binstub webpack, it's looking for the actual webpack). The reason it can't find it is because there's an issue with yarn:install:

# railties/lib/rails/tasks/yarn.rake
namespace :yarn do
  desc "Install all JavaScript dependencies as specified via Yarn"
  task :install do
    system("./bin/yarn install --no-progress --production") 
    #       ^^^^^^^^^^ There is no "yarn" binstub so this fails silently 
  end
end

# Run Yarn prior to Sprockets assets precompilation, so dependencies are available for use.
if Rake::Task.task_defined?("assets:precompile")
  Rake::Task["assets:precompile"].enhance [ "yarn:install" ]
end

As you can see above, the Rails 5.2 code is looking for ./bin/yarn, but that binstub doesn't exist and isn't install by webpacker—nor does bundle install --binstubs add a yarn binstub. This is where I believe the bug is.

The yarn:install task is added as an enhancement to assets:precompile, but given that the .bin/yarn command fails silently, node_modules is never installed, so node_modules/.bin/webpack is never added, which causes this error saying that it can't find the webpack stub.

Adding a manual yarn:install task to the project Rakefile that removes this breaking ./bin fixes the issue with deployment:

# Project Rakefile
Rails.application.load_tasks

namespace :yarn do
  desc "Install all JavaScript dependencies as specified via Yarn"
  task :install do
    system("yarn install --no-progress --production")
  end
end

@gauravtiwari I agree with @bbugh that this shouldn't be closed. I'm having the exact issue he described.

Hi, not sure if this is related but I ran into a similar issue (same logs "No such file or directory node_modules/.bin/webpack (Errno::ENOENT)") on a project that I updated from Rails 4.2 to Rails 5.2.

After digging, here is what was happening:

After adding the bin/yarn file (copied from a Rails 5 project), it worked!

@antoinematyja Your solution worked. For the sake of other developers, here is the content of yarn file that worked for me, by placing it in /bin filder.

#!/usr/bin/env ruby
APP_ROOT = File.expand_path('..', __dir__)
Dir.chdir(APP_ROOT) do
  begin
    exec "yarnpkg", *ARGV
  rescue Errno::ENOENT
    $stderr.puts "Yarn executable was not detected in the system."
    $stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install"
    exit 1
  end
end

@kolosek may the universe bless your existence. After a seemingly endless amount of turmoil I finally have assets precompiling on EB! Praise the Lord!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

johan-smits picture johan-smits  ·  3Comments

ilrock picture ilrock  ·  3Comments

suhomlineugene picture suhomlineugene  ·  3Comments

FrankFang picture FrankFang  ·  3Comments

suhomozgy-andrey picture suhomozgy-andrey  ·  3Comments