ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true

19.4k views Asked by At

I am facing this problem

Uglifier::Error: Unexpected token: keyword (const). To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true). while deploying the project through capistrano on production.

I followed this solution

https://github.com/lautis/uglifier/issues/127#issuecomment-352224986

which suggests

replacing

config.assets.js_compressor = :uglifier

with

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

but even after doing that I am still facing the same error. I dont understand what went wrong. I am using uglifier (4.1.20) version

4

There are 4 answers

0
Sritam On

Just leaving the answer here too:

In config/environments/production.rb replace

config.assets.js_compressor = :uglifier

with

config.assets.js_compressor = Uglifier.new(harmony: true)
1
Anton Maminov On

Uglifier only works with ES5. If you need to compress ES6, ruby-terser is a better option.

3
Eduardo Sztokbant On

On Rails 6.1, I managed to solve this issue by:

  1. Removing the uglifier gem from Gemfile.
  2. Removing the config.assets.js_compressor entry in config/environments/production.rb and config/environments/development.rb.

Ref.: https://www.mintbit.com/blog/rails-5-6-upgrade-es6-uglifier-bug

0
Sumit.Daksh On

We get this issue to upgrade 5.2 to 6.1

I fixed this issue to replace uglifier js_compressor with terser

config/environments/production.rb Replace

gem 'uglifier' # remove gem from Gemfile
config.assets.js_compressor = :uglifier
or
config.assets.js_compressor = Uglifier.new(harmony: true)

to

gem 'terser' # add gem in Gemfile
config.assets.js_compressor = :terser