bundle exec rake assets:precompile fails with `unexpected token`

11,898

Solution 1

I had this exact same problem and solved it, here are the details:

When you include one or more javascript files in your Rails 3.1 environment using application.js and //= require ..., Rails will wrap the contents of your file with a <script> ..filecontents.. </script>. You can verify this by running your site in development mode and doing a View | Source.

The problem is, in development mode, if you are not actually using that script, it may appear to work fine. But, Essentially, what you have is <script><script> ..filecontents.. </script></script>.

However, when you attempt to PRECOMPILE the assets, something in that compiling process (sorry - I do not know what exactly) gags on the "<" in the inner script token.

Double-check your included .js files and, if any of them are wrapped by <script> ... </script> remove those surrounding tags.

You should see that, in Development, everything still looks fine if you View Page Source. And, when you precompile your assets, the error should go away.

I ran into this problem with Google's recommended google-analytics javascript snippet and removing the script wrapper from the included file solved the problem.

Solution 2

Here's how I debugged this.

Locally, run this

RAILS_ENV=production bundle exec rake assets:precompile

Try to find out where it chokes (the script right before the choking is the file you'll want to look at). Look at all the requires.

In my case, I had a file that ended in .js when it should've been .jsx and that's what fixed it.

Share:
11,898
Adam Spiers
Author by

Adam Spiers

Long-term F/OSS hacker and musician, currently working for SUSE.

Updated on July 13, 2022

Comments

  • Adam Spiers
    Adam Spiers almost 2 years

    I'm ready to deploy my Rails 3.1 app into production, and since I'm using the asset pipeline, I need to precompile my assets. However, when I try this, I get an error apparently related to compiling jQuery:

    $ bundle exec rake --trace assets:precompile
    ** Invoke assets:precompile (first_time)
    ** Execute assets:precompile
    /home/adam/.rvm/rubies/ruby-1.9.3-p0/bin/ruby /home/adam/.rvm/gems/[email protected]/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets --trace
    ** Invoke assets:precompile:all (first_time)
    ** Execute assets:precompile:all
    ** Invoke assets:precompile:primary (first_time)
    ** Invoke assets:environment (first_time)
    ** Execute assets:environment
    ** Invoke environment (first_time)
    ** Execute environment
    ** Invoke tmp:cache:clear (first_time)
    ** Execute tmp:cache:clear
    ** Execute assets:precompile:primary
    rake aborted!
    399: unexpected token at '"/*!\u000a * jQuery JavaScript Library v1.7.1\u000a * http://jquery.com/\u000a *\u000a * Copyright 2011, John Resig\u000a * Dual licensed under the MIT or GPL Version 2 licenses.\u000a * http://jquery.org/license\u000a *\u000a * Includes Sizzle.js\u000a * http://sizzlejs.com/\u000a * Copyright 2011, The Dojo Foundation\u000a * Released under the MIT, BSD, and GPL Licenses.\u000a *\u000a * Date: Mon Nov 21 21:11:03 2011 -0500\u000a */\u000afunction addActiveScaffoldPageToHistory(a,b){if(typeof
    

    [snip lots of stuff]

      (in /data/music/RotC/eventbook/app/assets/javascripts/application.js)
    /home/adam/.rvm/gems/[email protected]/gems/json-1.6.4/lib/json/common.rb:148:in `parse'
    /home/adam/.rvm/gems/[email protected]/gems/json-1.6.4/lib/json/common.rb:148:in `parse'
    /home/adam/.rvm/gems/[email protected]/gems/multi_json-1.0.4/lib/multi_json/engines/json_common.rb:9:in `decode'
    /home/adam/.rvm/gems/[email protected]/gems/multi_json-1.0.4/lib/multi_json.rb:76:in `decode'
    /home/adam/.rvm/gems/[email protected]/gems/execjs-1.2.13/lib/execjs/external_runtime.rb:61:in `extract_result'
    /home/adam/.rvm/gems/[email protected]/gems/execjs-1.2.13/lib/execjs/external_runtime.rb:27:in `block in exec'
    /home/adam/.rvm/gems/[email protected]/gems/execjs-1.2.13/lib/execjs/external_runtime.rb:40:in `compile_to_tempfile'
    /home/adam/.rvm/gems/[email protected]/gems/execjs-1.2.13/lib/execjs/external_runtime.rb:26:in `exec'
    

    [snip lots more stuff]

    Here's my application.js:

    //= require jquery
    //= require jquery_ujs
    //= require jquery-ui
    //
    // N.B. jQuery requires have to come before this:
    //= require active_scaffold
    

    I'm using the default compressor, i.e. uglifier.

    I've found that if I change config.assets.compress to false in config/environments/production.rb then it works fine, but of course my app would perform better if I could figure out a way to keep it as true.

    I've looked at rake assets:precompile doesn't work (rails 3.1.1) and I don't think it's a duplicate because the error is undefined: Unexpected token: operator (<).

    Any ideas? I'm vaguely suspicious of those unicode characters in the jQuery code, but I'm not sure how to prove or disprove that they are causing the problem.

  • Adam Spiers
    Adam Spiers about 12 years
    Thanks a lot for taking the time to report this, Dave. Actually at some point since asking this question, the issue magically fixed itself for me - perhaps due my upgrade of Rails to 3.2 or one of the gems? However I'm accepting your answer because it sounds like a plausible solution for people still seeing the issue.
  • EricC
    EricC almost 10 years
    Thanks! I came across this in rails 3.2. Super helpful!
  • max pleaner
    max pleaner over 9 years
    did not solve my problem (i have no script tags except the application javascript include tag in application.html.erb
  • Hoang Le
    Hoang Le over 8 years
    Excellent answer! Thanks so much. I resolved my problem when one of my gems includes html file which has invalid <script> tab.
  • user229044
    user229044 about 8 years
    If you want to put comments in your JavaScript, use /* */ or //.
  • lucasarruda
    lucasarruda about 8 years
    Thanks for the tip. That came from hotjar embedded code, which some dev copied when it added hotjar.
  • Jacob Crofts
    Jacob Crofts about 7 years
    I had a similar problem, but my unexpected token was >. Turns out I was writing ES6 in an app that didn't support ES6.