'The asset "application.js" is not present in the asset pipeline

I'm new in Ruby and in ROR framework. I try to create web app with JQuery. My steps are:

  1. Added gem 'jquery-rails' and executed bundle install command from command line
  2. String //= require jquery2 added into application.js file
  3. Now I try include file into page. And I added in section string <%= javascript_include_tag "application" %> into application.html.erb file

when I try to see page in browser I see error with message:

Sprockets::Rails::Helper::AssetNotFound in LandingPage#index
The asset "application.js" is not present in the asset pipeline.
<%= javascript_include_tag "application" %>

What I doing wrong? I googled about this error, but I didn't found anything good explained.



Solution 1:[1]

I also ran in this error with Rails 6 (with webpack) using the syntax javascript_include_tag in the application.html.erb file.

With rails 6 and webpack use:

<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

Solution 2:[2]

If you're running into this problem with a Rails app deployed to Heroku, it's possible that you need to specify the file name.

This is for an app that was previously a Rails 6 app but as I upgraded to Rails 7, the application.erb was no longer recognizing my application.js file.

I changed this:

    <%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>

to this:

    <%= javascript_include_tag "/assets/application.js", "data-turbo-track": "reload", defer: true %>

and that fixed my Heroku production problem.

Solution 3:[3]

The accepted solution above resolves the issue for most but if like me you continued to have problems after following every instruction and every solution. The below might help you.

After going down a rabbit hole of errors, I found the following works.

In my experience, its to do with webpacker and your nvm installation. To try the solution, start by reverting any other actions you did to debug the issue. You need to ensure that you are running the latest webpacker version.

I would then run:

    rails webpacker:compile 

in your terminal.

If it compiles without a node warning then all good. if it gives you a warning about your node version, and or it does not compile, install the latest LTS node version. mine is 16.13.1. for ubuntu. . This is important. The latest version of node is not necessarily the latest lts version. You need to install in your home directory. Run:

    nvm install --lts
    nvm use --lts

You may want to set this as your default eg. :

    nvm alias default 16.13.1

Also ensure that your application layout has:

    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

Once that is done run webpacker compile again as above.

If you have any issues after this you can also try adding below in your config / initializer / assets.rb file:

    Rails.application.config.assets.precompile += %w(application.js)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rudolf Fäder
Solution 2 iarobinson
Solution 3 arnold bwaila