Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Rails 4.2 webpacker erb extension raises an error #1382

Closed
mdesantis opened this issue Mar 27, 2018 · 8 comments
Closed

On Rails 4.2 webpacker erb extension raises an error #1382

mdesantis opened this issue Mar 27, 2018 · 8 comments

Comments

@mdesantis
Copy link
Contributor

mdesantis commented Mar 27, 2018

Hello,

I tried with a new Rails 4.2.10 webapp (Ruby 2.3.6, webpacker 3.4.1). This is the error (running bin/webpack):

> bin/webpack
Running via Spring preloader in process 21336
(erb):1:in `<main>': undefined method `javascript_pack_tag' for main:Object (NoMethodError)
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/2.3.0/erb.rb:864:in `eval'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/2.3.0/erb.rb:864:in `result'
	from /home/user/Development/rails-4-2/demo/node_modules/rails-erb-loader/erb_transformer.rb:19:in `<top (required)>'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/railties-4.2.10/lib/rails/commands/runner.rb:60:in `load'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/railties-4.2.10/lib/rails/commands/runner.rb:60:in `<top (required)>'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `require'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `block in require'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:240:in `load_dependency'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `require'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:123:in `require_command!'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:90:in `runner'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/railties-4.2.10/lib/rails/commands.rb:17:in `<top (required)>'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `require'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `block in require'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:240:in `load_dependency'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `require'
	from /home/user/Development/rails-4-2/demo/bin/rails:9:in `<top (required)>'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:268:in `load'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:268:in `block in load'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:240:in `load_dependency'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:268:in `load'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:59:in `require'
	from /home/user/.asdf/installs/ruby/2.3.6/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:59:in `require'
	from -e:1:in `<main>'
Hash: d3154deb032db5c9ea00
Version: webpack 3.11.0
Time: 1003ms
                                  Asset       Size  Chunks             Chunk Names
      hello_erb-b52939fd96b9147f831a.js    3.18 kB       0  [emitted]  hello_erb
    application-6d6fda76e45f26772dbc.js    3.26 kB       1  [emitted]  application
  hello_erb-b52939fd96b9147f831a.js.map    2.49 kB       0  [emitted]  hello_erb
application-6d6fda76e45f26772dbc.js.map    3.19 kB       1  [emitted]  application
                          manifest.json  274 bytes          [emitted]  
   [0] ./app/javascript/packs/application.js 515 bytes {1} [built]
   [1] ./app/javascript/packs/hello_erb.js.erb 415 bytes {0} [built] [failed] [1 error]

ERROR in ./app/javascript/packs/hello_erb.js.erb
Module build failed: Error: rails-erb-loader failed with code: 1
    at ChildProcess.<anonymous> (/home/user/Development/rails-4-2/demo/node_modules/rails-erb-loader/index.js:108:16)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at maybeClose (internal/child_process.js:925:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)

I have a reproducing script (it uses asdf in order to set Ruby version, feel free to change it):

#!/bin/bash

set -e

mkdir rails-4-2
cd rails-4-2
which asdf >/dev/null 2>&1 && asdf local ruby 2.3.6 || true
echo "gem 'rails', '~> 4.2.0'" >> Gemfile
bundle install
rails new demo
cd demo
echo "gem 'webpacker', '3.4.1'" >> Gemfile
bundle install
bundle exec rake webpacker:install
bundle exec rake webpacker:install:erb
bin/webpack
@gauravtiwari
Copy link
Member

gauravtiwari commented Mar 27, 2018

@mdesantis I commented it out earlier but seems like it sneaked back in. Could you please comment out <%%= javascript_pack_tag > in comment at the top (with two %%) in hello_erb.js.erb file?

@mdesantis
Copy link
Contributor Author

mdesantis commented Mar 27, 2018

Hi @gauravtiwari , your workaround worked, the issue disappeared transforming the <%= javascript_pack_tag 'hello_erb' %> in the comment into <%#= javascript_pack_tag 'hello_erb' %>. And the reason seems even obvious to me now. Perhaps hello_erb.js.erb template should be modified?

@gauravtiwari
Copy link
Member

Great 👍 yep this was fixed earlier but seems like it got changed later. Could you please make a PR with the fix and I will merge?

@mdesantis
Copy link
Contributor Author

mdesantis commented Mar 27, 2018

What if I modify the hello_erb.js.erb heading comment into this?

// Run this example by adding <%%= javascript_pack_tag 'hello_erb' %> to the head of your layout file,
// like app/views/layouts/application.html.erb. Please take care of removing the extra `%` from `<%%=`:
// we can't write the correct ERB tags here, they would get interpreted.

Sounds good? Can you please check if it is a correct sentence? My english is bad :)

@gauravtiwari
Copy link
Member

@mdesantis Looks about right. If you make a PR we can tweak it there 👍 thank you

@VRRmax
Copy link
Contributor

VRRmax commented Mar 25, 2020

I don't understand why you use <%%= instead of <%#= which is the standard way for ERB commenting.
<%%= looks like a typo.

@mdesantis
Copy link
Contributor Author

mdesantis commented Mar 26, 2020

@VRRmax if I remember correctly, the reason is that otherwise that line gets interpreted by the generator which would raise an error at Webpacker installation. So <%%= is the ERB escaped form of <%=

@VRRmax VRRmax mentioned this issue Mar 26, 2020
@thebravoman
Copy link

Just want to let you know the error still exists as of today for a different case

(erb):7:in <main>': undefined method javascript_pack_tag' for main:Object (NoMethodError

For those moving from application.js to application.js.erb note that you have this in the application.js.erb

/* eslint no-console:0 */
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
//
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb

and it should be changed to this

/* eslint no-console:0 */
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
//
// To reference this file, add <%%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants