4

After adding the ERB loader and adding the .erb file extension to my application pack (with webpacker), I am getting the following error:

ERROR in ./app/webpack/packs/application.js.erb
Module build failed: Error: spawn bin/rails ENOENT
    at _errnoException (util.js:1024:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
    at onErrorNT (internal/child_process.js:372:16)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
 @ multi (webpack)-dev-server/client?http://localhost:3035 ./app/webpack/packs/application.js.erb

it is also happening with the sample hello_erb.js.erb pack.

Here is an example app that reproduces the problem: https://github.com/jonhue/test_app

heroxav
  • 1,387
  • 1
  • 22
  • 65
  • Do you have the same issue if you set up a new blank Rails project with minimal configuration? – Arctodus Feb 14 '18 at 19:47
  • @Sharagoz Yep ... – heroxav Feb 14 '18 at 21:04
  • Ok, could you provide some information about your platform? Like operating system, NodeJS version, Yarn version, Ruby version – Arctodus Feb 15 '18 at 08:42
  • @Sharagoz Yarn: `1.3.2`, Windows 10 (latest updates), NodeJS: `8.9.3`, Ruby: `2.3.3p222 (2016-11-21 revision 56859) [i386-mingw32]`, Rails: `5.1.5` – heroxav Feb 15 '18 at 10:13
  • 1
    The ERB runner configuration might not work for windows .Try changing the runner option in `config\webpack\loaders\erb.js` to `runner: "ruby bin\\rails runner"`. – Arctodus Feb 16 '18 at 13:15
  • @Sharagoz Seems like that did work. At least I am getting another error now: `ERROR in ./app/javascript/packs/hello_erb.js.erb Module build failed: Error: Command failed: ruby bin\rails runner E:\projects\apps\test_app\node_modules\rails-erb-loader\erb_transformer.rb __RAILS_ERB_LOADER_DELIMETER__ erb 0 (erb):1:in `
    ': undefined method `javascript_pack_tag' for main:Object (NoMethodError) [...]`
    – heroxav Feb 16 '18 at 15:41
  • @Sharagoz You can find the `hello_erb.js.erb` pack here: https://github.com/jonhue/test_app/blob/master/app/javascript/packs/hello_erb.js.erb – heroxav Feb 16 '18 at 15:43
  • Line 1 and 2 in that file got ERB syntax in the comment. Try removing those lines. – Arctodus Feb 16 '18 at 16:50
  • @Sharagoz :D And I just assumed it would work, as it's a generated example. Thanks for your help! Webpack compiles now. Create an answer so I can award the bounty :-) – heroxav Feb 16 '18 at 18:32
  • Answer created, glad I could help! – Arctodus Feb 16 '18 at 18:49

2 Answers2

5

You may be missing generated files

If you are missing the bin/rails executable maybe you have cloned from a repository that followed the steps on this issue Your enviroment should be good to go if you have rails installed correctly and then run:

Rails < 5

$ bundle exec rake rails:update:bin

Rails >= 5

$ rails app:update:bin

You may have issues with webpack installation and/or configuration

If those files on bin\ exists, you may have ran into problems when installing and/or configuring webpack. Try creating a project installing everything following the documentation, commiting it with a versioning system like GitHub, copy/paste your project configuration and git diff it, I bet you will find differences between them.

You are using Windows or some enviroment that is problematic

If that is the case there is already a discussion to fix it in an open issue in webpacker github. But you could try stuff like using webpacker directly from github to get latest fixes, downgrading to other versions to see if the result is different, or use webpack from source and change the file install.rake that makes you call ./bin/rails app:template... to just rails app:template....

ErvalhouS
  • 4,178
  • 1
  • 22
  • 38
  • Hm.. I do have a `rails` executable in my `bin` folder. `bundle exec rake rails:update:bin` fails however. I listed all tasks available for `rake` and `rails` is not one of them. – heroxav Feb 14 '18 at 06:01
  • 1
    Seems like that command only works in rails 4. I ran `rails app:update` and forced the binstubs to update. Still getting the same error. Small note: webpack complained and I had to run `bundle binstubs bundler --force` too. – heroxav Feb 14 '18 at 06:10
  • You misunderstood me: I am still getting the same error - even after updating my binstubs... – heroxav Feb 14 '18 at 15:07
  • I did setup a sample app using the following steps: (1) `rails new test_app --webpack` (2) `bundle exec rails webpacker:install:erb` (3) `bundle binstubs bundler --force` (4) `./bin/webpack-dev-server` -> Error – heroxav Feb 14 '18 at 21:06
  • You should run `bundle exec rails webpacker:install` before `bundle exec rails webpacker:install:erb`, you skipped a step. – ErvalhouS Feb 14 '18 at 21:13
  • Creating a new Rails app with the `--webpack` flag automatically runs `bundle exec rails webpacker:install` since 5.1 – heroxav Feb 15 '18 at 05:56
  • I published an example app the reproduced the problem: https://github.com/jonhue/test_app – heroxav Feb 15 '18 at 17:48
  • Where do I find `install.rake`? – heroxav Feb 15 '18 at 19:36
  • https://github.com/rails/webpacker/blob/d9cf446d3a901c3a8c76fc998ac9d4c7a04f443e/lib/tasks/webpacker/install.rake – ErvalhouS Feb 15 '18 at 19:41
2

The ERB runner configuration contains a setting that must be changed for it to work on Windows. Open config\webpack\loaders\erb.js and replace this line:

runner: "bin/rails runner"

with this line:

runner: "ruby bin\\rails runner
Arctodus
  • 5,743
  • 3
  • 32
  • 44
  • Do you know a way in which I can specify the runner option to work both on Windows & Linux? – heroxav Feb 18 '18 at 13:23
  • @jonhue There's no built in way that I know of, but you might be able to use an initialize to dynamically generate the file or use a custom runner that calls the real runner depending on what `Gem::Platform.local.os` yields – Arctodus Feb 19 '18 at 13:58
  • Solution is fairly obvious. I created separate erb-loader configuration files for development & production. – heroxav Feb 19 '18 at 15:08