Skip to content

Webpacker in production (Webpacker can't find application in...) #2071

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

Closed
ycrepeau opened this issue Apr 28, 2019 · 29 comments
Closed

Webpacker in production (Webpacker can't find application in...) #2071

ycrepeau opened this issue Apr 28, 2019 · 29 comments

Comments

@ycrepeau
Copy link

This is a reccurent issue. I have spent about 10 days trying to figure out. In last and desperate measure, I have tried to create a new fresh project with "Rails new test-vue4 webpacker=vue".

  1. Precompiled assets (and by extension webpack stuff)
  2. Started the server in production mode.

As usual, I have:

F, [2019-04-28T16:05:34.858805 #23809] FATAL -- : [92e6eb8c-8b5f-40cd-9fac-4ab36c32025c] ActionView::Template::Error (Webpacker can't find application in /Users/ycrepeau/Document/Developpement/test-vue4/public/packs/manifest.json. Possible causes:
1. You want to set webpacker.yml value of compile to true for your environment
   unless you are using the `webpack -w` or the webpack-dev-server.
2. webpack has not yet re-run to reflect updates.
3. You have misconfigured Webpacker's config/webpacker.yml file.
4. Your webpack configuration is not creating a manifest.
Your manifest contains:
{
  "application.js": "/packs/js/application-9171bbec65575b6edf4f.js",
  "application.js.map": "/packs/js/application-9171bbec65575b6edf4f.js.map",
  "entrypoints": {
    "application": {
      "js": [
        "/packs/js/application-9171bbec65575b6edf4f.js"
      ],
      "js.map": [
        "/packs/js/application-9171bbec65575b6edf4f.js.map"
      ]
    },
    "hello_vue": {
      "css": [
        "/packs/css/hello_vue-da095892.css"
      ],
      "js": [
        "/packs/js/hello_vue-5cee8bc0b69a0e8ff7f4.js"
      ],
      "js.map": [
        "/packs/js/hello_vue-5cee8bc0b69a0e8ff7f4.js.map"
      ]
    }
  },
  "hello_vue.css": "/packs/css/hello_vue-da095892.css",
  "hello_vue.js": "/packs/js/hello_vue-5cee8bc0b69a0e8ff7f4.js",
  "hello_vue.js.map": "/packs/js/hello_vue-5cee8bc0b69a0e8ff7f4.js.map"
}
):
F, [2019-04-28T16:05:34.858946 #23809] FATAL -- : [92e6eb8c-8b5f-40cd-9fac-4ab36c32025c]      8:     %title= content_for?(:title) ? yield(:title) : "Untitled"
[92e6eb8c-8b5f-40cd-9fac-4ab36c32025c]      9: 
[92e6eb8c-8b5f-40cd-9fac-4ab36c32025c]     10:     = javascript_pack_tag 'application'
[92e6eb8c-8b5f-40cd-9fac-4ab36c32025c]     11:     = stylesheet_pack_tag 'application'
[92e6eb8c-8b5f-40cd-9fac-4ab36c32025c]     12:     = stylesheet_link_tag "application"
[92e6eb8c-8b5f-40cd-9fac-4ab36c32025c]     13:     = javascript_include_tag "application", "data-turbolinks-track" => true
[92e6eb8c-8b5f-40cd-9fac-4ab36c32025c]     14:     = csrf_meta_tag
F, [2019-04-28T16:05:34.858984 #23809] FATAL -- : [92e6eb8c-8b5f-40cd-9fac-4ab36c32025c]   
F, [2019-04-28T16:05:34.859059 #23809] FATAL -- : [92e6eb8c-8b5f-40cd-9fac-4ab36c32025c] app/views/layouts/application.html.haml:11:

As you can see, the manifest.json is there. The related js and css files are there too.

What puzzles me is the line:

 (Webpacker can't find application in /Users/ycrepeau/Document/Developpement/test-vue4/public/packs/manifest.json. 

when the manifest.json is present at that path.

The expected behaviour is simply to have a message typed in the console. By default (out of the box), app/javascript/packs/application.js is a very simple "hello world" example.

The <% =javascript_pack_tag 'application'%> seems to be blind in production mode, unable to "see" the manifest.json file which is obviously present.

I use node 10.15.3 and ruby 2.6.3. Webpacker: 4.0.2

The package.json:

{
  "name": "test-vue4",
  "private": true,
  "dependencies": {
    "@rails/webpacker": "^4.0.2",
    "vue": "^2.6.10",
    "vue-loader": "^15.7.0",
    "vue-template-compiler": "^2.6.10"
  },
  "devDependencies": {
    "webpack-dev-server": "^3.3.1"
  }
}

No issue in development mode or test mode.

@ycrepeau
Copy link
Author

I think I've found the culprit: sass
I don't have any "pure css" in my vue file. I use sass/scss.

The sass loader does not extract the css code. Everything seem to be embeded in the js file. So, when I call <%=stylesheet_pack_tag 'application'%> webpack tries to locate application.css in the manifest.json BUT the .css is absent.

I STRONGLY SUGGEST to add some warning when used in development mode. Something like: WARNING: We are tolerant here in development mode but watch out once your app is deployed.

@andhart
Copy link

andhart commented Apr 29, 2019

Ran into this same issue, commenting out <%= stylesheet_pack_tag 'application', turbolink_track: true %> did seem to solve it, but I agree, this should have a warning.

@ncphillips
Copy link
Contributor

ncphillips commented May 7, 2019

We just discovered this issue as well. We had some packs that didn't have any css assoicated with them, so there was no file generated.

IMO the problem is that <%=stylesheet_pack_tag 'application'%> blows up if it can't find a css file for application. I would prefer it either handle missing css files by:

  • not outputting anything, or
  • output a style tag without the hash and let the browser 404 when looking for it

@jakeNiemiec
Copy link
Member

#2096 could be related to this.

@ycrepeau
Copy link
Author

Solution found (not application.css generated, remove the <%=stylesheet_pack_tag%>). I close this issue.

andyw8 added a commit to andyw8/rails_6_example that referenced this issue May 23, 2019
@mstate
Copy link

mstate commented Jul 1, 2019

To address this issue I set

extract_css: true

under production in webpacker.yml

Then, when running rake assets:precompile, make sure to have RAILS_ENV=production. In my case, that led to the css files being created for me during pre-compilation, and the error no longer appeared.

@JacksonBates
Copy link

@ycrepeau you are my hero.

I've been trying to fix this prod bug for far too many days now, and it was the errant stylesheet pack tag causing the problem.

I spent so much time assuming the JS wasn't compiling properly it didn't occur to me that CSS was the issue.

@Siva-Ganesh-VR
Copy link

@jhuarancca
Copy link

I removed this line
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
from application.html.erb and It worked

Best regards

@xuanchien
Copy link

xuanchien commented Dec 28, 2019

This issue drove me crazy. It turns out the issue was because I created a folder called css under app/javascript. I renamed that folder into stylesheets and it now works as I expected. Took me a few days to think and figure this out

@MtnBiker
Copy link

MtnBiker commented Jan 4, 2020

@xuanchien. Your suggestion worked for me. But I can't be absolutely sure that did it. I was fiddling with other webpacker settings. But I'll leave well enough alone.

@xuanchien
Copy link

@MtnBiker I have learned a few things that can cause this issue:

  1. A folder named css under app/javascript will break this for sure.


  2. You don't reference a CSS file from your JS pack file. It must be CSS, not SCSS or others. If I have the following line in my pack


import('stylesheets/site')

Then my referenced CSS file must be site.css, it cannot be site.scss

  1. Forgot to change the extract_css option to true in webpacker.yml

@MtnBiker
Copy link

MtnBiker commented Jan 5, 2020

@xuanchien. This is such a mess in Rails. stylesheets under javascript, etc; but it's what we have.

  1. I followed one of the blogs and css was more familiar to me as a folder, but stylesheets it is.

  2. Are you saying nothing named .scss will be used? I have ignored the styling trying to work on other aspects of what I'm doing. I just tested some of this. In packs/application.js I have the line import '../stylesheets/application' and it works for application.scss.

And in application.scss I have (among others

@import 'bootstrap_custom'; // adding .scss may not have helped it load
@import "~bootstrap/scss/bootstrap.scss";

and it works. I confirmed by commenting out and my styling disappeared.

  1. I still have false. What does changing to true do?

@xuanchien
Copy link

If you still have extract_css as false, it means the CSS is not extracted to a separate file (the JS file will load it asynchronously). Thus stylesheet_pack_tag is not used by Rails (you can remove it and see that it still works). You can try changing it to true and then check if point 2 still works or not.

@MtnBiker
Copy link

MtnBiker commented Jan 6, 2020

Got it. Thanks for the info. Will have to work through stylesheets with Webpacker again. Maybe someone will post again on how to do it right and was is the best/acceptable way with Rails. I looked at several posting and lots of contradictions.

@jakeNiemiec
Copy link
Member

I followed one of the blogs...I looked at several posting and lots of contradictions.

The official docs should be up to date. If you find contradictions, please open an issue with details.

You can find more info on stylesheets with Webpacker over here: https://github.com/rails/webpacker/blob/master/docs/css.md#css-sass-and-scss

@MtnBiker
Copy link

MtnBiker commented Jan 7, 2020

Thanks for the link. I did not see it because it was at the bottom of the page. Maybe the second paragraph of the ReadMe should mention what's not covered in the ReadMe and to see below for what's else is available. https://guides.rubyonrails.org/asset_pipeline.html has no mention of Webpacker. The first example on the css-sass-and-scss page is "When you do import '../scss/application.scss'" which is confusing based on the discussion in this thread. What's missing for me is something like a Rails Guides discussion on how to structure and what needs to be imported. A beginner's guide to best practices and when to deviate. As I said earlier webpacker seems to violate basic Rails structure: having scss under a folder named javascript. The things @xuanchien mentioned in this thread aren't in the docs. Where would I find that information, for example?

Thanks for your patience.

@jakeNiemiec
Copy link
Member

"When you do import '../scss/application.scss'" which is confusing based on the discussion in this thread.

As it states in https://github.com/rails/webpacker#usage you must import your styles in one of your webpack entry files under /packs. As long as you don't put the scss or css in /packs, you can probably import it from anywhere.

A beginner's guide to best practices and when to deviate.

I agree that the readme could be better structured.


The things @xuanchien mentioned in this thread aren't in the docs. Where would I find that information, for example?

Lets take a look. First off, xuanchien is right about extract_css. You can read about that from the file I linked.

And in application.scss I have (among others) ... // adding .scss may not have helped it load

Where is application.scss located? Is it imported via JS anywhere?

A folder named css under app/javascript will break this for sure.

If true, this is a bug. Something like this should work:

// Folder structure
// app/javascript:
// ├── packs:               👈 only webpack entry files here!
// │   └── application.js   👈 this file is below
// └── css:
// │   └── site.css
// └── images:
// └── logo.svg
// this file is application.js
// These should all work (only pick one)
import '../css/site';
import '../css/site.css';
import './../css/site.css';
import 'app/javascript/css/site.css';

...You don't reference a CSS file from your JS pack file. It must be CSS, not SCSS or others. If I have the following line in my pack import('stylesheets/site')

You don't need to use the extension if the file is unambiguous. Look in your webpacker.yml for the order these resolve in. Here is the default: https://github.com/rails/webpacker/blob/master/lib/install/config/webpacker.yml#L36

seems to violate basic Rails structure: having scss under a folder named javascript.

This is because Rails is letting the javascript compile the scss instead of the asset pipeline.

Hopefully this quick, meandering post helps illuminate things.

@MtnBiker
Copy link

MtnBiker commented Jan 9, 2020

This helps. I'll be working at it more as I'm still having related issues. I may SO post soon about an issue with Leaflet. ( L.timeline is not a function).

Thank you.

@dennyf
Copy link

dennyf commented Jan 30, 2020

I was experiencing the same issue when running the server in production mode. I finally managed to get it work, and just for the record - I'm not sure if this is the right way to do it, but it works for me. So, here is what I have:

  1. Folder structure:
app/javascript:
├── packs:
│   └── application.js
└── stylesheets:
│   └── application.scss
└── images:
    └── all images
  1. In app/javascript/packs/application.js
import '../stylesheets/application'
require.context('../images', true)
  1. In app/views/layouts/application.html.erb:
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
  1. In config/webpacker.yml extract_css is true for production:
extract_css: true
  1. Then I run the following before starting the server in production mode:
RAILS_ENV=production rails assets:precompile
  1. After following the steps above it worked a few times, then at one point it stopped working and I started getting the same error again. This time just deleting the contents of the public/packs folder resolved the issue (I had some files from the previous builds). I think that maybe manifest.json wasn't getting regenerated properly and when I got it deleted from the public/packs folder, it was forced to get regenerated.

I hope this helps!

dcmorse added a commit to contradb/contra that referenced this issue Feb 1, 2020
ActionView::Template::Error (Webpacker can't find search_page in /home/rails/contra/public/packs/manifest.json ...
rails/webpacker#2071 looks like the solution
@imechemi
Copy link

If the public/packs directory does not exist in your project, then you have to run

     bundle exec rails webpacker:install

You will now see transpiled files as below and you will no longer see the issue.

public
├── packs
   ├── js
   │   ├── application-bbe9c4a129ab949e0636.js
   │   └── application-bbe9c4a129ab949e0636.js.map
   └── manifest.json

@bbugh
Copy link
Contributor

bbugh commented May 12, 2020

FYI you don't have to do the "import css in javascript" thing since Webpacker 5.0 introduced multiple pack entry points with the same name. The documentation for this does not seem to have been updated yet.

Instead of import '../your_styles/application.scss in your application.js file, you can just make a packs/application.scss file and webpacker will correctly handle it.

@ibantoo
Copy link

ibantoo commented May 27, 2020

i have same issue as well,

ActionView::Template::Error (Webpacker can't find src/application in /home/gallery/gallery/releases/20200526160750/public/packs/manifest.json. Possible causes:
1. You want to set webpacker.yml value of compile to true for your environment
   unless you are using the `webpack -w` or the webpack-dev-server.
2. webpack has not yet re-run to reflect updates.
3. You have misconfigured Webpacker's config/webpacker.yml file.
4. Your webpack configuration is not creating a manifest.
Your manifest contains:
{
  "application.css": "/packs/css/application-6cf3a592.css",
  "application.js": "/packs/js/application-36acab8d3b78d3c9970e.js",
  "application.js.map": "/packs/js/application-36acab8d3b78d3c9970e.js.map",
  "entrypoints": {
    "application": {
      "css": [
        "/packs/css/application-6cf3a592.css"
      ],
      "js": [
        "/packs/js/application-36acab8d3b78d3c9970e.js"
      ],
      "js.map": [
        "/packs/js/application-36acab8d3b78d3c9970e.js.map"
      ]
    },
    "navbar": {
      "js": [
        "/packs/js/navbar-9e6a1b1d3aaf831d3a6f.js"
      ],
      "js.map": [
        "/packs/js/navbar-9e6a1b1d3aaf831d3a6f.js.map"
      ]
    }
  },
  "navbar.js": "/packs/js/navbar-9e6a1b1d3aaf831d3a6f.js",
  "navbar.js.map": "/packs/js/navbar-9e6a1b1d3aaf831d3a6f.js.map"
}
):

i already did what people said above but still can't resolve that in production :(
i am using capistrano for asset:precompile

it looks that my rails read in the src/application ,
how i can change to be in application ?

trantrungtin95 pushed a commit to trantrungtin95/food that referenced this issue Sep 20, 2020
boddhisattva added a commit to boddhisattva/recipebook that referenced this issue Oct 17, 2020
Tried solving it through the below solutions but of no avail thus far:
- https://stackoverflow.com/a/59162932/272398
- https://stackoverflow.com/a/58507538/272398
- rails/webpacker#2071
- https://stackoverflow.com/a/62239847/272398

 As mentioned here: https://prathamesh.tech/2019/08/26/understanding-webpacker-in-rails-6/, webpacker can be used to compile javascript code but in order to include in our apps we use the javascript_pack_tag. It feels ok to remove javascript_pack_tag for now as for the current scope of the given problem statement javascript_pack_tag is not something we need to make use of anytime soon.

I've also posted a question here: https://stackoverflow.com/questions/59161189/webpacker-4-2-cant-find-application-in-app-public-packs-manifest-json-heroku/59162932#comment113793109_59162932 to follow up on this later.
maxshelley pushed a commit to Sundae-Club/sundaeclub that referenced this issue Nov 15, 2020
@selcuktoklucu
Copy link

Is there any other suggestion other than comments above? I'm still having this issue when I deploy the application. I've been trying to figure this out for weeks, and still working on it. Our Rails app was 6.0.3.2 and I'm getting this error after I upgraded to 6.1.1 only in Production Environment. And, In my local env in production mode, if I call RAILS_ENV=production bundle exec rails webpacker:compile and then start the server, there is no problem. However, if I set compile: true in webpacker.yml for production, and let it compile in the first GET request, it doesn't compile in the same way and throws this error again. Since our app is on heroku, I can't workaround with webpacker:compile command (I tried adding to deployment commands).

@ycrepeau
Copy link
Author

I have a working example running:
Ruby 2.7.2
Node 12.20.1
Gemfile content compatible with Ruby 2.7.2
Package.json compatible with Node 12.20.1
A Dockerfile used to build and run the project.
The project (source code) is here: https://gitlab.com/ycrepeau/rail611
The (compiled) Docker image is: ycrepeau/rails611:v1

To run the the Ror server: docker run -p 3000:3000 ycrepeau/rails611:v1

I have another project and I wanted to move from Ruby 2.6.2 to Ruby 2.7.2. Since I was using ruby-alpine image, changing the ruby version also changed the Node version (10 to 12). And the "big fun" started there. I have to look for every node_module correct version compatible with my setup. Once I have found the right versions for both the Gemfile and Package.json, everything worked fine.

So, to look the project, git clone the project and use docker build to watch every step involved.

//TODO: Write the .gtlab-ci.yml to use Gitlab CI/CD and deploy the project to Heroku.

@ycrepeau
Copy link
Author

One important issue: With Rails 6.1.1, check file: /config/environment/production.js, around line 25.

config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

should be replaced by
config.public_file_server.enabled = true

@serv
Copy link

serv commented Mar 15, 2021

I haven't fully solved the issue yet, but one thing I found is $ bin/rails webpacker:compile step can exit with code 0 (without error), even though webpack compiled with some errors. For example, something like this can happen.

webpack 5.25.1 compiled with 8 errors in 4778 ms

As a result, your Rails app may attempt to render a page without incomplete front-end build, and the CSS portion might be missing.

To make matters even more deceptive, you might get something like

$ bin/rails webpacker:compile
Everything's up-to-date. Nothing to do

This makes it seem like there's no problem with the webpacker compilation result. However, in reality, it might have just cached the webpacker compilation result from previous run with webpack errors! In order to see true webpack compilation error messages, you might want to do rm -rf tmp/cache

Make sure to check bin/rails webpacker:compile results in a webpack compilation without errors before you do anything.

@feliperaul
Copy link
Contributor

feliperaul commented May 17, 2021

For future googlers: you are probably importing a pack that doesn't have any CSS imported inside it, and stylesheet_pack_tag or stylesheet_packs_with_chunks_tag will raise an exception in production because in that environment extract_css: true in webpacker.yml.

Like I wrote in #3022, the problem is:

  • The default extract_css config in webpacker.yml in development and test (!) envs is false, so it will only bite in production;
  • It shouldn't be up to the developer to know if the imported packs have or don't have any CSS being imported; specially in large packs, it's very difficult to follow the graph to check them all, and, again, if someone adds a new pack to the import that doesn't have any CSS, it will only explode in production;

I solved my issue by creating a service object that reads manifest.json (in real time in development to avoid server restarts, but totally memoized in production for max performance) and filters out any packs that don't have any css attached with them.

  1. Replace in your layouts all stylesheet_packs_with_chunks_tag with this new method we'll create, named safe_stylesheet_packs_with_chunks_tag.

  2. Define this in your ApplicationHelper:

def safe_stylesheet_packs_with_chunks_tag(*array_of_pack_names)

    stylesheet_packs_with_chunks_tag(*WebpackerPacksService.safe_additional_webpacker_css_entry_packs_to_load(*array_of_pack_names))

  end
  1. In your app/services (create folder if it doesn't exist), create this webpacker_packs_service.rb file:
module WebpackerPacksService

  def self.webpacker_entry_packs_with_css

    if Rails.env.development?
      calculate_webpacker_entry_packs_with_css
    else
      @webpacker_entry_packs_with_css ||= calculate_webpacker_entry_packs_with_css
    end

  end

  def self.calculate_webpacker_entry_packs_with_css

    JSON.load(File.read(Rails.root.join("public/packs/manifest.json")))['entrypoints'].select {|pack_name, manifest| manifest['css'].present? }.keys

  end

  def self.safe_additional_webpacker_css_entry_packs_to_load(*array_of_pack_names)

    @safe_additional_webpacker_css_entry_packs_to_load ||= {}

    return @safe_additional_webpacker_css_entry_packs_to_load[array_of_pack_names] if @safe_additional_webpacker_css_entry_packs_to_load.has_key?(array_of_pack_names)

    @safe_additional_webpacker_css_entry_packs_to_load[array_of_pack_names] = array_of_pack_names & webpacker_entry_packs_with_css

  end

end

@SimonSomlai
Copy link

SimonSomlai commented Oct 6, 2022

For me it was related to having the /public/packs ignored in my .gitignore. Which was done by the rails webpacker:install command

After I removed that, everything worked on heroku!

@rails rails deleted a comment Feb 6, 2025
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