Why does Rails fails to boot with "Expected to find a manifest file in `app/assets/config/manifest.js` (Sprockets::Railtie::ManifestNeededError)"?
After bundle update
my Rails app fails to boot with:
Expected to find a manifest file in `app/assets/config/manifest.js` (Sprockets::Railtie::ManifestNeededError)
What's happening?
Looks like you've upgraded sprockets. The later version(s) of sprockets require what's called a manifest.js
file. You don't have one. You need to create one, and add in a few "directives".
Why do I need to do this?
In the old version of sprockets, big assumptions were made about what assets to bundle/concatenate. things were implicit. it wasn't clear what was happening: kinda a big headache for developers when they wanted to tweak something. The latest changes are a step in the right direction. These old assumptions are gone. The designers now want you to tell sprockets explicitly, what files you want bundled and/or concatenated, and you do this in your manifest.js file e.g.:
- bundle everything in folder
abc
together - bundle AND concatenate everything in folder
xyz
- keep
admin.js
separate.
Easy Steps To Solve the Problem:
-
Create the manifest.js file
$ mkdir -p app/assets/config $ touch app/assets/config/manifest.js (not the root rails directory)
-
Then copy and paste the following into the manifest.js file you just created:
//= link_tree ../images //= link_directory ../javascripts .js //= link_directory ../stylesheets .css
Those funny commenty things above //=
are called "directives". Sprockets says:
"ok ima grab every js
file in the javascripts directory, concatenate them, and keep them as SEPARATE javascript files i.e. no bundling."
Now we have some processed files, we can send along with our html, so that our users can have a great js/css experience. You should have a javascript_include_tag
which hands over your js files to users when they ask for it. If you have other files, don't forget to add them to application.html.erb
.
-
If you have a precompile array in your
app/config/environments/projection.rb
folder (see below for an example) then perhaps you should move them over to yourmanifest.js
if they are not already accessed above.config.assets.precompile = ["admin.js", "admin.css"]
Presumably you will want your admin.js
javascript file separate from your application.js
file. No problem, just tell sprockets to keep them separate:
//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
//= link "admin.js"
- Lastly, if you are using webpacker, you might want to decide what you want handled by the asset pipeline and what you want handled by webpacker. i.e. remove the link_directory to the javascripts file according to your own particular use cases.
Reference: read here for further details re: manifest.js. file
Source: Thanks to Richard Schneeman's blog - see here for more information..
EDIT: folks, if things are confusing or not clear: how can I fix this answer if you don't comment and tell me where/what is not clear? everyone benefits by these improvements, so pls speak up.
A new major version of sprockets was recently released which is not compatible with the previous version.
Either perform the steps needed to upgrade or pin to version 3.x in Gemfile
gem 'sprockets', '~>3.0'
Based on the answer here you may be able to solve this with:
mkdir -p app/assets/config && echo '{}' > app/assets/config/manifest.js
And if you need more details, the answer in this thread helpfully points to the Guide to upgrading from Sprockets 3.x to 4.x