简体   繁体   中英

Ruby on Rails Error: Uglifier::Error: Unexpected token: operator (<)

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?

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM