57

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

Ashish Jambhulkar
  • 1,374
  • 2
  • 14
  • 26
  • Rails 6 is moving towards Webpack and away from Sprockets, so you may want to see if that approach works for you instead. – tadman May 09 '19 at 15:42
  • Did you ever get this working? I'm having the same issue. And do have the harmony option set – Jake McAllister Feb 17 '21 at 08:37
  • This is very strange. Has anyone found a solution that does not involve getting rid of Uglifier? – Yohann Sep 10 '21 at 15:07
  • We switched to the Terser gem, as per the recommendation and it worked well. We had to use `rails assets:clobber` and re-deploy to avoid a permissions issue in `tmp/cache/assets/sprockets`. – Joshua Pinter Sep 27 '22 at 16:03

4 Answers4

121

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)
Viktor
  • 2,623
  • 3
  • 19
  • 28
Sritam
  • 3,022
  • 1
  • 18
  • 17
7

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

  • TY @Eduardo Sztokbant I had to remove uglifier to fix this issue on my Rails 6.1.5 app. It wasn't an issue until I added the TinyMCE Editor. Once I did that and tried to push to Heroku it coughed up a lung in that nasty error message you have posted here. I tried the first solution it didn't work. So I removed Uglifier all together. Not only did it fix the issue but JavaScript is 5x faster now. Whatever the built in mechanism for the Asset pipeline with sprockets (I removed webpacker) seems to do a better job than Uglifier did. – Scott Milella Jul 13 '22 at 22:52
  • 2
    Note: this is only a valid solution if you've upgraded your app to use Webpacker (the default asset compiler in Rails 6) instead of sprockets. – mltsy Oct 21 '22 at 20:07
  • I'm using an older version of Rails (5.2.8) and deploying to Heroku and this solution worked for me. – J.R. Bob Dobbs Jun 05 '23 at 00:36
5

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

Anton Maminov
  • 463
  • 1
  • 6
  • 11
0

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