Rails Webpacker and Sass undefined variable error
This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.
I've recently started using Webpacker with Rails as an alternative to the asset pipeline. In addition to compiling my Javascript assets I've opted to pipe my Sass through it as well, so everything is going through the one place. However, I've been running into some undefined variable
issues as Webpacker compiles the Sass stylesheets. Interestingly enough it doesn't prevent the build completing but it's still just an annoying error.
It turns out this was because of the location I was placing my Sass files. My pack was located in an app/javascript/packs/application.js
and in it I had the following relative import:
What you'll need to do it move your styles up a directory, into just app/javascript/styles
. You can then adjust the import to grab it from the parent directory instead.
The boilerplate for the blank application.js
file acually does warn you that the app/javascript/packs
directory should only be used to create entry points to your packs, so taking notice of this would have prevented this and also shows how moving your assets up a directory is the expected solution.