One fine day when I was having a chit chat with my colleagues in the cafeteria, I received the email notification which I was waiting for. Yes, my next project’s scope document. The use case is as exciting as it got. After the discussions with my fellow teammates, when I was about to start the application using Rails 5, I received a message from my Boss who wanted me to try the project with Rails 6.
I have always been a hesitating man to try out new things as when things go haywire, I need people to support the issues. Still, I wanted to break it by using Rails 6 as the first person to try it out in my whole office. At that time I was not aware of the crisis that I was going to face.
Before that let me brief you what is a webpacker!
How Webpacker attracted me!
Being a guy who worked with the static and slow web applications, Rails 6 with Webpacker was an eye-opener for me. If I do any changes in JS (or) CSS file, for the first time it takes some time to compile and serve. After that, I was able to feel the difference which is my application was loading very fast and blaze when compared to the previous Rails versions. Yes, this is how Webpacker made me believed that it is improving quality.
How Webpacker started showing its face to me!
In previous rails versions, we used to import application.js and application.css like below.
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
This is how it starts. Normally, in application.js, we have to import application.scss. When I import application.scss in packs, it failed to compile the SCSS and CSS file. After a long search, I found out what could work:
i.e: Remove the
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> from application.html.erb. When I removed the line, the application starts working fine and it started compiling CSS and SCSS files.
Every developer has this habit: If the application works fine even with spaghetti and unreadable code, they won’t touch it again. That’s what I did 😀
Then came the day when I was supposed to go push the application to the staging server. When I moved the application from development to staging, the demon again started laughing at me. Yes, I ran into an issue again. This time the application was in the staging server. It was kept on throwing an issue that webpacker cant find application.scss. It asked me to include the
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> in application.html.erb. If I include the above line, then the development server won’t work. So, I predicted that this is not the right solution for the issue.
I started reading a lot of blogs and documentations to find out the solution. But I can’t find one for the two long days. Then I came up with a workaround to overcome this issue.
1. In application.html.erb, I decided to check whether the application is running in production mode or development mode. If it is in production mode I, have to include the
<%= stylesheet_pack_tag 'application' %>
I felt this will solve my issue temporarily but not permanently. This started making me feel more guilty and I was again into searching for the right solution. This time, I have asked a question in StackOverflow. Sadly, I didn’t receive any proper solution as Rails 6 is new. so I didn’t get the Many of the answers suggested me to change the application.scss path but it doesn’t work in my case and I have also reinstalled Webpacker, via Rails and Yarn in different ways. Still no clue.
At last, I found the right solution after a day. I just renamed my stylesheets/application.scss into stylesheets/style.scss. And in application.js I have imported like:
That’s it. The application worked like a charm after the deep struggle.
Putting it all together, the actual solution is that if we have the same file name in the packs folder irrespective of the file types, it will compile the first file in the tree and another file will get ignored automatically by webpacker. But I’m still wondering why webpacker is not looking at the file extension types. Anyhow, I’m happy that I got my solution right.
Though webpacker annoyed me to the core, I loved the challenge it has placed in front of me. And here I’m going to start my new project on Rails 6. Don’t worry. If I find the issues, I will share the solution for it like how I did with this blog. Happy coding !!! 🙂
Also, leaving below the StackOverflow questions for reference: https://stackoverflow.com/questions/58506351/webpacker-throws-application-css-not-found-in-manifest-json-in-rails-6-applica
6 Replies to “How WebPacker and Rails 6 Tortured Me and how…”
Nice article. But I have run into the same problem and hacked it by commenting out the lines
So there is a deeper issue at play here ?
Hey Arminius thank you for spending some time to read my blog. By commenting out the lines it will work now. But in future case you may face an issue. Its better to avoid commenting the lines. Instead of that import stylesheets in application.js. Happy coding mate.
Thanks for this article!
I read it several times, but still can’t solve this problem.
I’m newbie in programming, maybe that’s why 🙂
Anyway… that’s what I finally did (after reading your article):
What I had before changes:
1) application.css (inside the file: *= require ‘./main’) (that’s because someone told me, that it’s a bad idea to change application.css to application.scss)
After reading your article:
And after RAILS_ENV=production bundle exec rake assets:precompile
i got -> field ‘browser’ doesn’t contain a valid alias configuration error
Did I something wrong or I just misunderstood the article?
Thank your for this article. I really appreciate that!
Regards from Poland
I created the rail application today i got the same error.
Ruby – 3.0.0
Rails – 6.0.1
Many times i thought est see whats there under rails 6 but it was always blocking me.
Today i just went with
$ rails new pokeman
same error i encountered
cd public and then $ rake assets:precompile solved my issue.
Now good to explore rails 6.
Seemed promising, but didn’t work for me, unfortunately.. My app works after `rake webpacker:compile` and `rails s` in production mode locally, but it doesn’t work when I deploy on heroku, and getting this error with empty manifest.json object, in the error message.
I continue to be tortured by this. I’ve tried EVERYTHING, at least 3x. This has got to be a bug with Yarn, Webpacker to Node.js or all of them. Incredibly frustrated.