2

Whenever I run rake assets:precompile RAILS_ENV=production in my command line, I get this error which prevents me from precompiling my assets.

Below is the complete error displayed in the command line:

rake aborted!
Uglifier::Error: Unexpected token: operator (<)
/usr/local/rvm/gems/ruby-2.3.4/gems/uglifier-4.1.4/lib/uglifier.rb:234:in parse_result'
/usr/local/rvm/gems/ruby-2.3.4/gems/uglifier-4.1.4/lib/uglifier.rb:216:in run_uglifyjs'
/usr/local/rvm/gems/ruby-2.3.4/gems/uglifier-4.1.4/lib/uglifier.rb:168:in compile'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/compressing.rb:65:in block in js_compressor='
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/legacy_proc_processor.rb:31:in call'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:75:in call_processor'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:57:in block in call_processors'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:56:in reverse_each'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:56:in call_processors'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/loader.rb:134:in load_from_unloaded'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/loader.rb:60:in block in load'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/loader.rb:317:in fetch_asset_from_dependency_cache'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/loader.rb:44:in load'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/cached_environment.rb:20:in block in initialize'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/cached_environment.rb:47:in load'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/base.rb:66:in find_asset'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/base.rb:73:in find_all_linked_assets'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:142:in block in find'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:114:in block (2 levels) in logical_paths'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:228:in block in stat_tree'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:212:in block in stat_directory'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:209:in each'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:209:in stat_directory'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:227:in stat_tree'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:105:in each'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:105:in block in logical_paths'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:104:in each'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:104:in logical_paths'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:140:in find'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:185:in compile'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:68:in block (3 levels) in define'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/rake/sprocketstask.rb:147:in with_logger'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:67:in block (2 levels) in define'
/usr/local/rvm/gems/ruby-2.3.4/gems/rake-12.3.0/exe/rake:27:in <top (required)>'
/usr/local/rvm/gems/ruby-2.3.4/bin/ruby_executable_hooks:15:in eval'
/usr/local/rvm/gems/ruby-2.3.4/bin/ruby_executable_hooks:15:in <main>'
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)

What exactly do I need to do to fix this error?

Sheikh Hasib
  • 7,423
  • 2
  • 25
  • 37
  • You'll likely need the full trace to track down the issue - see the note at the bottom of your current one: `See full trace by running task with --trace`. Try running this and see if it points you to the error, if not, update the question with the full trace and someone should be able to help :) – SRack May 25 '18 at 12:17

3 Answers3

13

I encountered the same error. Interestingly, it was triggered by the direct_upload.js file that Rails itself recommends for ActiveStorage! I found the solution here: https://github.com/lautis/uglifier/issues/127

Try replacing

config.assets.js_compressor = :uglifier

with

config.assets.js_compressor = Uglifier.new(harmony: true)

in config/environments/production.rb

mindtonic
  • 1,385
  • 2
  • 14
  • 24
  • 1
    Thank you! This would have driven me crazy. You're callout of the relationship with direct_upload.js was exactly my situation. – acoustic_north Feb 14 '19 at 21:17
1

You can always check the JS files by parsing it with the uglifier compiler. Open your rails console

const JS_FOLDER = [file1_path, file2_path];
JS_FOLDER.each do. |file_name|
  puts "#{file_name}"
  puts  Uglifier.compile(File.read(file_name))
end

And there you will have your corrupted file.

teju c
  • 336
  • 2
  • 8
0

The trace is indicating that you have a syntax error in one of your JavaScript files. If you look in your browser's console, it might tell you where the error is.

If not, I'd suggest removing JS files until you find the culprit, and then commenting out lines in the offending file until you find the error. A binary search method might be helpful; start by removing roughly half your JS files and then trying the precompilation. If the error still occurs, then remove half of what's left and retry. When the error stops occurring, then you know which group of files contains the offending file, so you can back up one step, remove everything but that group, and try recompiling again. Eventually you should figure out which file contains the error.

Once you've narrowed it down to a single file, you can use the same technique by commenting out about half the lines in the file, recompile, etc.

Brian
  • 5,300
  • 2
  • 26
  • 32
  • This was unfortunately the issue for me. The error didn't show itself in the browser because the corrupted file wasn't part of that page, so I was blaming it on Uglifier. The previous error complained about ES6 syntax, which in hindsight should have signaled syntax error. – Torrey Payne Apr 21 '19 at 20:05