Webpacker: Getting undefined method `javascript_pack_tag' for ...

Created on 15 Mar 2017  路  33Comments  路  Source: rails/webpacker

Having Rails 5 application and followed this tutorial https://medium.com/statuscode/introducing-webpacker-7136d66cddfb#.4xpiogt9l how to install and run webpacker.

Having webpacker in Gemfile

gem 'webpacker', github: 'rails/webpacker'

And used installation commands

rails webpacker:install
rails webpacker:install:react

And when open page with <%= javascript_pack_tag 'app' %> I am getting undefined method 'javascript_pack_tag' for #<#<Class:0x007f8d5a158378>:0x007f8d5a0015b0>

./bin/webpack-dev-server runs fine (but having some babel errors, but I guess that can't have impact on previous error).

Most helpful comment

Don't forget to restart your server :trollface:

All 33 comments

It works on my side. I'm using rails 5.1.0.beta1, latest webpacker from github.

Did you bundle install?

Not sure what error you got for webpack-dev-server, but if your port is blocked, you will encounter error like [Error] Failed to load resource: the server responded with a status of 404 (File Not Found) (hello_react.js, line 0). The solution is to edit development.rb and change the port from 8080 to something else (ie 8081)

Let me know if the problem is resolved.

@ytbryan Sure I run bundle install

Using webpacker 1.0 from git://github.com/rails/webpacker.git (at master@a85a3ab)

Tutorial said it is for Rails 4.2+ and unfortunately due to dependency hell in my project I can't update to 5.1 yet :(

I traced the gem source code webpacker/lib/webpacker/railitie.rb

class Webpacker::Engine < ::Rails::Engine
  initializer :webpacker do |app|
    p "Initializing webpacker"
    ActiveSupport.on_load :action_controller do
      p "Using Webpacker::Helper"
      ActionController::Base.helper Webpacker::Helper
    end

and it printed my debug, but still same error.

Then I tried to use Helper manually and everything is fine.

class ApplicationController < ActionController::Base
  helper Webpacker::Helper
end

May I know which rails version is this project on?

@ytbryan Rails 5.0.1

Just tested it with 5.0.1 and it still works fine on my end.

Maybe some conflict gem?

I don't have enough info to confirm that.

Can you close this if the manual helped fixes it? Feel free to open it again if you find the root of the problem.

Thanks

@schovi Were you using .js.erb extension for app pack?

If yes to above, do you got javascript comments in that file? Please remove that since it's erb. I have made a demo for you here - https://tranquil-inlet-61365.herokuapp.com/ and code is here - https://github.com/gauravtiwari/webpacker-demo-app

@gauravtiwari I have bare structure generated by rails webpacker:install and rails webpacker:install:react nothing more.
And I think problem is strictly related to Rails and its Helpers, because of manaully using

class ApplicationController < ActionController::Base
  helper Webpacker::Helper
end

works fine.

Ah right, but it's bit strange too, given you are using Rails 5.0.1. It works without including helper for me - https://github.com/gauravtiwari/webpacker-demo-app. Perhaps, application.rb is setup differently - I mean includes on the top that adds gems from Rails?

@guilleiguaran

require_relative 'boot'

require 'active_record/railtie'
require 'action_controller/railtie'
require 'action_view/railtie'
# require 'action_mailer/railtie'
# require 'action_cable/engine'
# require 'rails/test_unit/railtie'
require 'sprockets/railtie'
require 'sidekiq/api'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module MyApp
...

@schovi Could you please add require 'webpacker/railtie' in the list and then retry running without including helper in the controller? The one I have got require 'rails/all', which means it adds everything.

@gauravtiwari

When adding require 'webpacker/railitie' it end up on missing constant Webpacker

/Users/schovi/.rvm/gems/ruby-2.4.0@lotof/bundler/gems/webpacker-a85a3ab0eac6/lib/webpacker/source.rb:5:in `<top (required)>': uninitialized constant Webpacker (NameError)
    from /Users/schovi/.rvm/gems/ruby-2.4.0@lotof/bundler/gems/webpacker-a85a3ab0eac6/lib/webpacker/helper.rb:1:in `require'
    from /Users/schovi/.rvm/gems/ruby-2.4.0@lotof/bundler/gems/webpacker-a85a3ab0eac6/lib/webpacker/helper.rb:1:in `<top (required)>'
    from /Users/schovi/.rvm/gems/ruby-2.4.0@lotof/bundler/gems/webpacker-a85a3ab0eac6/lib/webpacker/railtie.rb:3:in `require'
    from /Users/schovi/.rvm/gems/ruby-2.4.0@lotof/bundler/gems/webpacker-a85a3ab0eac6/lib/webpacker/railtie.rb:3:in `<top (required)>'
    from /Users/schovi/work/lotof/config/application.rb:14:in `require'

This fixes it:

module Webpacker
end
require 'webpacker/railtie'

but still undefined method 'javascript_pack_tag'. Even when require 'rails/al' as default.

When I will have time, i will try to remove 3rd party gems one by one to find if there is conflict.

@schovi I see. It's require 'rails/all' actually. Perhaps, just a typo in comment.

@gauravtiwari just typo, sorry.

@schovi I am having the same issue, even after adding your snippet:

require_relative 'boot'

require 'yaml'
require 'rails/all'
module Webpacker
end
require 'webpacker/railtie'

Bundler.require(*Rails.groups)

Did you find a solution to this? (that does not involve monkey-patching/manually defining helpers)

@lbrito1 it was not solution. It was just experimenting with custom requiring. Snippet from before was workaround https://github.com/rails/webpacker/issues/172#issuecomment-287304427

I have the same error here with rails 5.0.1 and what solved to you didn't change here...

I'm not sure why, but I found this problem was introduced when I included gem 'jquery-rails' and bundled. Update: That seems to have just been a result of an earlier error showing first.

A generated Rails 5.1.rc1 with --webpacker is working fine. I've updated a Rails 5.0 app and installed the webpacker gem. At one point it was working and I'm not sure what change broke the javascript_pack_tag method. I've compared the generated app to my own and found a few inconsistencies. Fixing those inconsistencies hasn't fixed it though.

Here's the repo: https://github.com/archonic/limestone. My eternal gratitude if you can find what the issue is.

Update: It's the strangest thing. I can insert a byebug and call Webpacker::Helper.methods and it doesn't include any of the methods that are clearly defined in my local gem files which are clearly loaded by bundler. Until that's figured out I've "fixed" it by using javascript_include_tag Webpacker::Manifest.lookup('application.js'), 'data-turbolinks-track': 'reload'

I ran into this issue, too, and in my app traced it to a config.before_action definition in the Active Admin initializer. I dug into it, and my best understanding of the issue is that this was causing ActionController::Base to be loaded prematurely, and this was somehow preventing the Webpacker helpers from being loaded properly (they were available under ActionController::Base.helpers but not in the views).

@tylerhunt I have ActiveAdmin ofcourse.

I've also run into this issue on rails 5.1.0.rc2 the project is using active admin but it doesn't use any before_action configuration. I've not had a change to look into what is causing this as yet and the work around fixes the issue for now. If I get a chance to look into this more deeply I'll report back here.

Please could you all try latest master - bundle update webpacker. This issue is now fixed 馃帀 馃嵃

Fixed, thanks!

Don't forget to restart your server :trollface:

For prosperity:
I got a message in the console that I had to update yarn (yarn install) and that fixed the error.
(Rails 5.2.2, webpacker 3.5.5)

I ran into a similar issue when adding webpacker erb support.

For me (and hopefully anyone reading this), the issue was caused by the js-style comment (as suggested earlier by another comment):

// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb

The solution is to make sure there is no <% ... tags anywhere in the code, including JS comments.

I had this same problem. I just restarted my server and it worked.

I had to stop the server, then run bin/spring stop then rails s
Then, the javascript_pack_tag worked.
(Restarting the server wasn't sufficient.)

But what has me curious is that I didn't need to start bin/webpack-dev-server
(I thought that was my problem originally) I am curious when, and where, that is needed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

naps62 picture naps62  路  3Comments

suhomozgy-andrey picture suhomozgy-andrey  路  3Comments

itay-grudev picture itay-grudev  路  3Comments

pioz picture pioz  路  3Comments

eriknygren picture eriknygren  路  3Comments