Uglifier error during assets precompile

I’m seeing some errors trying to run rake assets:precompile on 2.1.0 (stable branch), which appears to be due to the use of const in vendor/assets/javascripts/caret_position.js and app/assets/javascripts/service-worker.js.erb.

remote:        2018-09-11 02:38:12 +0000 Compressing: vendor-1d7e15932a900dc4b8be0306d85eb870358f859af13e8bd86e4b7fa9d859e1f1.js
remote:        Compressing Javascript and Generating Source Maps
remote:        rake aborted!
remote:        Uglifier::Error: Unexpected token: keyword (const). To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true).
remote:        /tmp/build_d4c490df87465281635b6b03a0274f93/vendor/bundle/ruby/2.4.0/gems/uglifier-4.1.11/lib/uglifier.rb:234:in `parse_result'
remote:        /tmp/build_d4c490df87465281635b6b03a0274f93/vendor/bundle/ruby/2.4.0/gems/uglifier-4.1.11/lib/uglifier.rb:216:in `run_uglifyjs'
remote:        /tmp/build_d4c490df87465281635b6b03a0274f93/vendor/bundle/ruby/2.4.0/gems/uglifier-4.1.11/lib/uglifier.rb:178:in `compile_with_map'
remote:        /tmp/build_d4c490df87465281635b6b03a0274f93/lib/tasks/assets.rake:103:in `compress_ruby'
remote:        /tmp/build_d4c490df87465281635b6b03a0274f93/lib/tasks/assets.rake:150:in `compress'
remote:        /tmp/build_d4c490df87465281635b6b03a0274f93/lib/tasks/assets.rake:190:in `block (4 levels) in <top (required)>'
remote:        /tmp/build_d4c490df87465281635b6b03a0274f93/lib/tasks/assets.rake:160:in `block in concurrent?'
remote:        /tmp/build_d4c490df87465281635b6b03a0274f93/lib/tasks/assets.rake:183:in `block (3 levels) in <top (required)>'
remote:        /tmp/build_d4c490df87465281635b6b03a0274f93/lib/tasks/assets.rake:174:in `each'
remote:        /tmp/build_d4c490df87465281635b6b03a0274f93/lib/tasks/assets.rake:174:in `block (2 levels) in <top (required)>'
remote:        /tmp/build_d4c490df87465281635b6b03a0274f93/lib/tasks/assets.rake:160:in `concurrent?'
remote:        /tmp/build_d4c490df87465281635b6b03a0274f93/lib/tasks/assets.rake:170:in `block in <top (required)>'
remote:        /tmp/build_d4c490df87465281635b6b03a0274f93/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
remote:        Tasks: TOP => assets:precompile
remote:        (See full trace by running task with --trace)

It’s possible this something specific to my wacky environment (deploying to heroku), but I was able to resolve it using the fix suggested in the error:

diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake
index bdca7c30ed..847a8d07c2 100644
--- a/lib/tasks/assets.rake
+++ b/lib/tasks/assets.rake
@@ -95,6 +95,7 @@ def compress_ruby(from, to)
   data = File.read("#{assets_path}/#{from}")

   uglified, map = Uglifier.new(comments: :none,
+                               harmony: true,
                                source_map: {
                                  filename: File.basename(from),
                                  output_filename: File.basename(to)
3 Likes

Yeah, you are on quite a pain train here :slight_smile: moving to DigitalOcean is my recommendation, our base image ships nodejs which rolls in features that are harmony as it goes

My guess, your node version is old

5 Likes

Which version of uglify are you running?

npm -g list uglify-js

It should be lower than 3.0.

3 Likes

I had the same error while upgrading from 2.0.3 to 2.1.0. We use our own Docker images

Updating to nodejs10 (from node8) didn’t fix the problem, but pinning uglify-js to the lastest 2.8 version fixed it ! Thanks @michaeld

3 Likes

Could you please explain precisely what change you applied to pin the uglify-js version (in Gemfile.lock I guess)…? :blush:
Thanks!

It’s not a gem, it’s a node module.

Something along the lines of

npm install -g uglify-js@"<3"
1 Like

I ran into this myself recently on a different platform and solved it in a similar manner to OP.
Although uglify-js is itself a node module, the uglifier gem bundles the code itself and executes it directly with ExecJS.
However (see lib/tasks/assets.rake) the gem is only used if the node version is not available (or if it’s disabled).
So the version of uglifier specified in Gemfile.lock is simply incompatible with some of discourse’s JS, unless harmony mode is used. This is probably worth fixing or else removing entirely.
In any case, I now see that the expected situation is to install node and uglify-js so that Discourse uses that.