I\'m trying to precompile my assets for production, but rails doesn\'t seem to be cooperating.
$ bundle exec rake assets:precompile
/home/drderp/.rvm/rubies/
My problem was with two success calls.
First was mqtt onSuccess
:
onSuccess() {
console.log("mqtt connected")
};
that I solved with:
onSuccess: function() {
console.log("mqtt connected")
};
and the second was Ajax success. Linter will show you
Error: Expected method shorthand
but everything will work properly.
This was horrible; no javascript error on my local computer means that there's an issue with asset compilation.
Here's how I solved it.
As suggested in the comments for a similar question ExecJS::ProgramError: Unexpected token punc «(», expected punc «:» when running rake assets:precompile on production , I removed uglifier (in production.rb
, comment out the compressor line) and compression altogether. (I had tried with other compressors; yui did not give much information. closure seemed to give me a hint but didn't help much)
I then compressed the assets locally, then I pushed to production.
(To compress locally, use RAILS_ENV=production rake assets:precompile
)
I ran on the server and this is when the javascript errors surfaced. It was basically erroneous merging of files (mainly due to something commented out). I got rid of those lines and pushed back to production. Everything succeeded.
I brought back uglifier, deleted the precompiled assets under public/assets/
and pushed back to production.
I hope this helps someone!
For some reason, on Heroku and also when I ran the above-mentioned code to "uglify" the javascript in the rails console, it would not report the line number of the error!. It is just a ruby wrapper for UglifyJS, which you can use online here https://skalman.github.io/UglifyJS-online/.
So I just copied and pasted the offending js file in here, and it reported the line number. Very easy.
In my case, I was using es6 syntax ()=>{...}
in js file.
Replacing it with function(){...}
fixed the problem.
Just a heads up on this, I had same issue and what was happening, when precompiling assets in production environment (and pushing to Heroku) several of my JS files were being inserted with foreign characters, along the lines of...
<<<<<<<<HEAD
===========
>>>>>>(random alphanumeric key)
I just ran a global search in my site directory for "<<" and quickly found the affected files & deleted these terms - everything worked fine.
For us, it was a weird little thing which leads us to actual underlying error, hence solution.
We had uglifier gem v 4.1.x
and latest at the time of writing is 4.2.x
we updated the uglifier version, just in case.
And what happened was that this new version actually started spitting out actual file locations where the error of compilation was happening. And when we know where the error is coming from, we fix it.
The actual issue was that we had some js.erb
files and these files were running some rails code to populate some env data, something like:
const config = {
abc: <%= Figaro.env.abc %>,
xyz: <%= Figaro.env.xyz %>
}
And the error we were getting was: Uglifier::Error: Unexpected token: punc (,)
.
So it was obvious for some reason Figaro was not giving us our desired values. We went on to fix that and checked our assumption by hardcoding random values, which compiled the JS successfully.
Putting it here so it may help someone.
PS: Top answer is great, but for some reason, it gave us some random jsx related errors to us instead of giving us an actual error.