uglifier: Uglifier::Error: Unexpected token: punc ((). To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true)

Hi, When I run “rake assets:precompile RAILS_ENV=production” command, I have issue. rake assets:precompile RAILS_ENV=production rake aborted! Uglifier::Error: Unexpected token: punc ((). To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true). /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/uglifier-4.0.2/lib/uglifier.rb:237:in parse_result' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/uglifier-4.0.2/lib/uglifier.rb:219:in run_uglifyjs’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/uglifier-4.0.2/lib/uglifier.rb:169:in compile' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/uglifier_compressor.rb:53:in call’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/uglifier_compressor.rb:28:in call' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:75:in call_processor’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:57:in block in call_processors' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:56:in reverse_each’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:56:in call_processors' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/loader.rb:134:in load_from_unloaded’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/loader.rb:60:in block in load' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/loader.rb:317:in fetch_asset_from_dependency_cache’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/loader.rb:44:in load' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/cached_environment.rb:20:in block in initialize’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/cached_environment.rb:47:in load' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/base.rb:66:in find_asset’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/base.rb:73:in find_all_linked_assets' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:142:in block in find’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:114:in block (2 levels) in logical_paths' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:228:in block in stat_tree’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:212:in block in stat_directory' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:209:in each’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:209:in stat_directory' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:227:in stat_tree’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:105:in each' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:105:in block in logical_paths’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:104:in each' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:104:in logical_paths’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:140:in find' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:185:in compile’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:68:in block (3 levels) in define' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-3.7.1/lib/rake/sprocketstask.rb:147:in with_logger’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:67:in block (2 levels) in define' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/gems/rake-12.2.1/exe/rake:27:in <top (required)>’ /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/bin/ruby_executable_hooks:15:in eval' /Users/leonverspeek/.rvm/gems/ruby-2.3.1@zombie/bin/ruby_executable_hooks:15:in <main>’ Tasks: TOP => assets:precompile (See full trace by running task with --trace)

How to fix this issue?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 42
  • Comments: 31 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Try replacing

config.assets.js_compressor = :uglifier

with

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

in config/environments/production.rb

abraham, you save my day

Thank you, that worked! - Is there any reason why I wouldn’t want to enable ES6 syntax or why this feature isn’t on by default?

@fwolfst Just use a rake task. This only checks the asset pipeline files. You may also want to check your vendor file. I had this issue and found out that eslint was converting a vendor file into es6 syntax.

namespace :javascript do
  desc "Compiles each js file"
  task validate: :environment do
    JS_PATH = "app/assets/javascripts/**/*.js"
    Dir[JS_PATH].each do |file_name|
      puts "\n#{file_name}"
      puts Uglifier.compile(File.read(file_name))
    end
  end
end

thanks! was using duck duck go to search and couldnt find anything. switched to google and it was on the first page…

Try replacing

config.assets.js_compressor = :uglifier

with

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

in config/environments/production.rb

@abraham You are a superhero.

Try replacing

config.assets.js_compressor = :uglifier

with

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

in config/environments/production.rb

For some reason, this leads to:

rake aborted!
NameError: uninitialized constant Uglifier

Nevermind, this was just a case of missing something as simple as

require 'uglifier' in production.rb

Silly me.

Thanks abraham !

In case anyone comes here looking for a solution to a similar error:

Uglifier::Error: Unexpected token: keyword (extends)        
--        
  1 import { Controller } from "stimulus"        
  2        
  => export default class extends Controller {

I recommend following this solution and switch to terser-ruby

Repalced config.assets.js_compressor = :uglifier with

config.assets.js_compressor = Uglifier.new(harmony: true) in config/environments/production.rb

as suggested by @abraham abraham but i am encountering new error after this

Uglifier::Error: Unexpected token punc «;», expected punc «,»

Could you please suggest howto fix.

config.assets.js_compressor = Uglifier.new(harmony: true) didn’t work for me, but I going up 3 release levels from 1.X something it got a lot better. Try going to ‘4.0.2’.

Is there any way to figure out which source JS-file is the culprit? iiuc, the error happens while compressing javascript code, so in theory there should be a solution by cleaning up the code before compression, isnt it?

I ran into this issue because Prettier was formatting an old jQuery file and adding ES6 syntax. Just throwing this solution in there in case someone gets stuck. Just save the file without Prettier.

Repalced config.assets.js_compressor = :uglifier with

config.assets.js_compressor = Uglifier.new(harmony: true) in config/environments/production.rb

as suggested by @abraham abraham but i am encountering new error after this

Uglifier::Error: Unexpected token punc «;», expected punc «,»

I’v got the same issus : Uglifier::Error: Unexpected token: punc (😃 Do you fixed it finally ? Thank you 😃