执行RAILS_ENV=production bin/rails assets:precompile命令时遭遇Uglifier::Error错误求助
Fixing Uglifier::Error During Rails Production Asset Precompilation
Hey there, let's work through this Uglifier error you're facing when running RAILS_ENV=production bin/rails assets:precompile.
Context & Error Details
- Your
yarn install v1.22.19ran successfully:[1/4] Resolving packages... success Nothing to install. success Saved lockfile. ✨ Done in 0.12s. - The command fails with a
Uglifier::Error, full trace below:rails aborted! Uglifier::Error: /Users/dev/.rvm/gems/ruby-2.7.5/gems/uglifier-4.2.0/lib/uglifier.rb:291:in `parse_result' /Users/dev/.rvm/gems/ruby-2.7.5/gems/uglifier-4.2.0/lib/uglifier.rb:221:in `run_uglifyjs' /Users/dev/.rvm/gems/ruby-2.7.5/gems/uglifier-4.2.0/lib/uglifier.rb:166:in `compile' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/compressing.rb:65:in `block in js_compressor=' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/legacy_proc_processor.rb:31:in `call' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:75:in `call_processor' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:57:in `block in call_processors' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:56:in `reverse_each' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:56:in `call_processors' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/loader.rb:134:in `load_from_unloaded' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/loader.rb:60:in `block in load' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/loader.rb:44:in `load' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:20:in `block in initialize' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:47:in `load' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/base.rb:66:in `find_asset' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/base.rb:73:in `find_all_linked_assets' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:142:in `block in find' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:114:in `block (2 levels) in logical_paths' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:228:in `block in stat_tree' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:212:in `block in stat_directory' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:209:in `each' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:209:in `stat_directory' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:227:in `stat_tree' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:105:in `each' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:105:in `block in logical_paths' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:104:in `each' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:104:in `logical_paths' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:140:in `find' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:186:in `compile' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-rails-3.4.2/lib/sprockets/rails/task.rb:67:in `block (3 levels) in define' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-3.7.2/lib/rake/sprocketstask.rb:147:in `with_logger' /Users/dev/.rvm/gems/ruby-2.7.5/gems/sprockets-rails-3.4.2/lib/sprockets/rails/task.rb:66:in `block (2 levels) in define' /Users/dev/.rvm/gems/ruby-2.7.5/gems/railties-5.2.8.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /Users/dev/.rvm/gems/ruby-2.7.5/gems/railties-5.2.8.1/lib/rails/commands/rake/rake_command.rb:20:in `perform' /Users/dev/.rvm/gems/ruby-2.7.5/gems/railties-5.2.8.1/lib/rails/command.rb:48:in `invoke' /Users/dev/.rvm/gems/ruby-2.7.5/gems/railties-5.2.8.1/lib/rails/commands.rb:18:in `<main>' /Users/dev/.rvm/gems/ruby-2.7.5/gems/bootsnap-1.12.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require' /Users/dev/.rvm/gems/ruby-2.7.5/gems/bootsnap-1.12.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require' /Users/dev/Documents/insta-clone/bin/rails:11:in `<top (required)>' /Users/dev/.rvm/gems/ruby-2.7.5/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `load' /Users/dev/.rvm/gems/ruby-2.7.5/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `call' /Users/dev/.rvm/gems/ruby-2.7.5/gems/spring-2.1.1/lib/spring/client/command.rb:7:in `call' /Users/dev/.rvm/gems/ruby-2.7.5/gems/spring-2.1.1/lib/spring/client.rb:30:in `run' /Users/dev/.rvm/gems/ruby-2.7.5/gems/spring-2.1.1/bin/spring:49:in `<top (required)>' /Users/dev/.rvm/gems/ruby-2.7.5/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `load' /Users/dev/.rvm/gems/ruby-2.7.5/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<top (required)>' /Users/dev/Documents/insta-clone/bin/spring:16:in `<top (required)>' bin/rails:5:in `load' bin/rails:5:in `<main>' Tasks: TOP => assets:precompile (See full trace by running task with --trace)
Solutions to Try
Let's go through the most common fixes for this issue, starting with the simplest:
1. Check for Invalid JavaScript Syntax
Uglifier throws this error when it encounters unparseable JS code.
- Manually review recently modified files in
app/assets/javascriptsfor typos, missing brackets, or untranspiled ES6+ features (like arrow functions orlet/constwithout proper support) - To find the exact file causing the issue, temporarily disable minification in
config/environments/production.rb:
Re-run the precompile command—if it succeeds, you know the problem is with a file that Uglifier can't minify, and you can test files one by one to narrow it down.config.assets.js_compressor = Uglifier.new(mangle: false, compress: false, harmony: true)
2. Enable ES6 Support in Uglifier
Uglifier 4.x supports modern JS syntax, but you need to enable the harmony flag. Update your production config:
# config/environments/production.rb config.assets.js_compressor = Uglifier.new(harmony: true)
This allows Uglifier to process ES6+ code that might be triggering the parse error.
3. Clear Asset Cache and Recompile
Stale cached assets can lead to unexpected compression failures. Run these commands to reset:
RAILS_ENV=production bin/rails assets:clobber RAILS_ENV=production bin/rails assets:precompile
4. Update Uglifier or Verify Third-Party Library Compatibility
- Try updating Uglifier to the latest version compatible with your Rails 5.2 setup:
bundle update uglifier - If you added a new npm package recently, it might contain code that Uglifier can't handle. Try removing the package temporarily—if the error goes away, you'll need to either use an alternative library or transpile that package's code to ES5 before compression.
内容的提问来源于stack exchange,提问作者Zargham Masood
相关产品推荐
相关产品推荐

