# Uglifier 在资源预编译期间出错

**URL:** https://meta.discourse.org/t/uglifier-error-during-assets-precompile/96970
**Category:** Support
**Tags:** unsupported-install
**Created:** [2018年九月11日 17:55 UTC](https://meta.discourse.org/t/uglifier-error-during-assets-precompile/96970 "2018-09-11T17:55:20Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![tmm1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tmm1/32/81412_2.png) [@tmm1](https://meta.discourse.org/u/tmm1)
#### Post date: [2018年九月11日 17:55 UTC](https://meta.discourse.org/t/uglifier-error-during-assets-precompile/96970/1 "2018-09-11T17:55:20Z")

</div>

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`.

```plaintext
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
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)

```

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2018年九月11日 21:14 UTC](https://meta.discourse.org/t/uglifier-error-during-assets-precompile/96970/2 "2018-09-11T21:14:41Z")

</div>

Yeah, you are on quite a pain train here 🙂 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

---

<div class="post-metadata">

### Author: ![michaeld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michaeld/32/1594_2.png) [@michaeld](https://meta.discourse.org/u/michaeld)
#### Post date: [2018年九月12日 04:41 UTC](https://meta.discourse.org/t/uglifier-error-during-assets-precompile/96970/3 "2018-09-12T04:41:31Z")

</div>

Which version of uglify are you running?

```
npm -g list uglify-js

```

It should be lower than 3.0.

---

<div class="post-metadata">

### Author: ![julienvey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/julienvey/32/110984_2.png) [@julienvey](https://meta.discourse.org/u/julienvey)
#### Post date: [2018年九月12日 12:11 UTC](https://meta.discourse.org/t/uglifier-error-during-assets-precompile/96970/4 "2018-09-12T12:11:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![JimboJoe](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@JimboJoe](https://meta.discourse.org/u/JimboJoe)
#### Post date: [2018年九月15日 13:11 UTC](https://meta.discourse.org/t/uglifier-error-during-assets-precompile/96970/5 "2018-09-15T13:11:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![michaeld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michaeld/32/1594_2.png) [@michaeld](https://meta.discourse.org/u/michaeld)
#### Post date: [2018年九月16日 20:54 UTC](https://meta.discourse.org/t/uglifier-error-during-assets-precompile/96970/6 "2018-09-16T20:54:04Z")

</div>

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

Something along the lines of

```
npm install -g uglify-js@"<3"

```

---

<div class="post-metadata">

### Author: ![pjdc](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@pjdc](https://meta.discourse.org/u/pjdc)
#### Post date: [2019年一月24日 03:03 UTC](https://meta.discourse.org/t/uglifier-error-during-assets-precompile/96970/7 "2019-01-24T03:03:58Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2022年一月23日 02:43 UTC](https://meta.discourse.org/t/uglifier-error-during-assets-precompile/96970/8 "2022-01-23T02:43:36Z")

</div>



---

<div class="post-metadata">

### Author: ![tmm1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tmm1/32/81412_2.png) [@tmm1](https://meta.discourse.org/u/tmm1)
#### Post date: [2022年二月12日 22:52 UTC](https://meta.discourse.org/t/uglifier-error-during-assets-precompile/96970/9 "2022-02-12T22:52:17Z")

</div>

我的 `harmony: true` hack 不再起作用，只产生了一个晦涩的 uglifier 错误：

```plaintext
remote: Compressing: vendor-e356674b67559130cbeca4530a0a5a00e71144f5e7556902f8a5efc8ac3f2282.js
remote: rake aborted!
remote: Uglifier::Error:
remote: /tmp/build_519258be/vendor/bundle/ruby/2.7.0/gems/uglifier-4.2.0/lib/uglifier.rb:291:in `parse_result'
remote: /tmp/build_519258be/vendor/bundle/ruby/2.7.0/gems/uglifier-4.2.0/lib/uglifier.rb:221:in `run_uglifyjs'
remote: /tmp/build_519258be/vendor/bundle/ruby/2.7.0/gems/uglifier-4.2.0/lib/uglifier.rb:176:in `compile_with_map'
remote: /tmp/build_519258be/lib/tasks/assets.rake:135:in `compress_ruby'
remote: /tmp/build_519258be/lib/tasks/assets.rake:178:in `compress'
remote: /tmp/build_519258be/lib/tasks/assets.rake:388:in `block (6 levels) in <main>'
remote: /tmp/build_519258be/lib/tasks/assets.rake:199:in `log_task_duration'
remote: /tmp/build_519258be/lib/tasks/assets.rake:383:in `block (5 levels) in <main>'
remote: /tmp/build_519258be/lib/tasks/assets.rake:189:in `block in concurrent?'
remote: /tmp/build_519258be/lib/tasks/assets.rake:382:in `block (4 levels) in <main>'
remote: /tmp/build_519258be/lib/tasks/assets.rake:371:in `each'
remote: /tmp/build_519258be/lib/tasks/assets.rake:371:in `block (3 levels) in <main>'
remote: /tmp/build_519258be/lib/tasks/assets.rake:189:in `concurrent?'
remote: /tmp/build_519258be/lib/tasks/assets.rake:368:in `block (2 levels) in <main>'
remote: /tmp/build_519258be/lib/tasks/assets.rake:199:in `log_task_duration'
remote: /tmp/build_519258be/lib/tasks/assets.rake:367:in `block in <main>'

```

正如 @pjdc 指出的，`compress_ruby` 仅在未通过 `which terser` 检测到 node 版本时使用。在我的环境中，它确实缺失了：

```plaintext
~ $ node -v
v16.13.1
~ $ which terser
~ $

```

看来我现在需要 `npm install -g terser`。这不应该包含在 package.json 中吗？

---

<div class="post-metadata">

### Author: ![tmm1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tmm1/32/81412_2.png) [@tmm1](https://meta.discourse.org/u/tmm1)
#### Post date: [2022年二月12日 23:06 UTC](https://meta.discourse.org/t/uglifier-error-during-assets-precompile/96970/10 "2022-02-12T23:06:43Z")

</div>

我可以使用 [How to Install Global npm Packages on Heroku - simonewebdesign](https://www.simonewebdesign.it/how-to-install-global-npm-packages-on-heroku/) 中的技术在 Heroku 上安装 `terser`。
