RAILS_ENV=production下rake assets:precompile无法正常工作

36

我试图使用命令rake assets:precompile RAILS_ENV=production预编译资产,但总是会得到以下错误。

** Invoke assets:precompile (first_time)
    ** Execute assets:precompile
    /usr/bin/ruby /usr/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets --trace
    ** Invoke assets:precompile:all (first_time)
    ** Execute assets:precompile:all
    ** Invoke assets:precompile:primary (first_time)
    ** Invoke assets:environment (first_time)
    ** Execute assets:environment
    ** Invoke environment (first_time)
    ** Execute environment
    ** Invoke tmp:cache:clear (first_time)
    ** Execute tmp:cache:clear
    ** Execute assets:precompile:primary
    rake aborted!
    LoadError: cannot load such file -- uglifier
      (in /home/cool_tech/cool_tech/app/assets/javascripts/application.js)
    /usr/lib/ruby/gems/2.0.0/gems/activesupport-3.2.14/lib/active_support/dependencies.rb:251:in `require'
    /usr/lib/ruby/gems/2.0.0/gems/activesupport-3.2.14/lib/active_support/dependencies.rb:251:in `block in require'
    /usr/lib/ruby/gems/2.0.0/gems/activesupport-3.2.14/lib/active_support/dependencies.rb:236:in `load_dependency'
    /usr/lib/ruby/gems/2.0.0/gems/activesupport-3.2.14/lib/active_support/dependencies.rb:251:in `require'
    /usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/compressors.rb:31:in `registered_js_compressor'
    /usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/bootstrap.rb:18:in `block in run'
    /usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/compressors.rb:80:in `call'
    /usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/compressors.rb:80:in `compressor'
    /usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/compressors.rb:74:in `compress'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/processing.rb:265:in `block in js_compressor='
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/processor.rb:29:in `call'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/processor.rb:29:in `evaluate'
    /usr/lib/ruby/gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb:103:in `render'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/context.rb:193:in `block in evaluate'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/context.rb:190:in `each'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/context.rb:190:in `evaluate'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/bundled_asset.rb:26:in `initialize'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:252:in `new'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:252:in `build_asset'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/index.rb:93:in `block in build_asset'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/caching.rb:19:in `cache_asset'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/index.rb:92:in `build_asset'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:169:in `find_asset'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/index.rb:60:in `find_asset'
    /usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/static_compiler.rb:19:in `block in compile'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:219:in `block in each_logical_path'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:206:in `block (2 levels) in each_file'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:196:in `each'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:196:in `each_entry'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:204:in `block in each_file'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:203:in `each'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:203:in `each_file'
    /usr/lib/ruby/gems/2.0.0/gems/sprockets-2.2.2/lib/sprockets/base.rb:217:in `each_logical_path'
    /usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/static_compiler.rb:18:in `compile'
    /usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/assets.rake:56:in `internal_precompile'
    /usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/assets.rake:70:in `block (3 levels) in <top (required)>'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:240:in `call'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:240:in `block in execute'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:235:in `each'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:235:in `execute'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
    /usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:165:in `invoke'
    /usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/assets.rake:60:in `block (3 levels) in <top (required)>'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:240:in `call'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:240:in `block in execute'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:235:in `each'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:235:in `execute'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
    /usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:165:in `invoke'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:150:in `invoke_task'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:106:in `each'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:106:in `block in top_level'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:115:in `run_with_threads'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:100:in `top_level'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:78:in `block in run'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:176:in `standard_exception_handling'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:75:in `run'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/bin/rake:33:in `<top (required)>'
    /usr/bin/rake:23:in `load'
    /usr/bin/rake:23:in `<main>'
    Tasks: TOP => assets:precompile:primary
    rake aborted!
    Command failed with status (1): [/usr/bin/ruby /usr/bin/rake assets:precomp...]
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/file_utils.rb:54:in `block in create_shell_runner'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/file_utils.rb:45:in `call'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/file_utils.rb:45:in `sh'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/file_utils_ext.rb:37:in `sh'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/file_utils.rb:82:in `ruby'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/file_utils_ext.rb:37:in `ruby'
    /usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/assets.rake:12:in `ruby_rake_task'
    /usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/assets.rake:21:in `invoke_or_reboot_rake_task'
    /usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/assets.rake:29:in `block (2 levels) in <top (required)>'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:240:in `call'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:240:in `block in execute'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:235:in `each'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:235:in `execute'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
    /usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/task.rb:165:in `invoke'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:150:in `invoke_task'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:106:in `each'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:106:in `block in top_level'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:115:in `run_with_threads'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:100:in `top_level'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:78:in `block in run'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:176:in `standard_exception_handling'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/lib/rake/application.rb:75:in `run'
    /usr/lib/ruby/gems/2.0.0/gems/rake-10.2.2/bin/rake:33:in `<top (required)>'
    /usr/bin/rake:23:in `load'
    /usr/bin/rake:23:in `<main>'
    Tasks: TOP => assets:precompile

有人知道出了什么问题吗?谢谢。


你如何在application.js中加载它?你直接在那里引用它吗?你能展示一下你的application.js文件吗? - Holger Frohloff
5个回答

68

替换

rake assets:precompile RAILS_ENV=production

使用 rake assets:precompile 命令来预编译资产文件(RAILS_ENV=production bundle exec rake assets:precompile 是确切的 rake 任务)。

由于预编译只在生产模式下进行,因此无需明确指定环境。

更新:

尝试将以下行添加到您的 Gemfile 中:

group :assets do
  gem 'therubyracer'
  gem 'sass-rails', "  ~> 3.1.0"
  gem 'coffee-rails', "~> 3.1.0"
  gem 'uglifier'
end

然后运行bundle install

希望它能够正常工作 :)


1
只需尝试使用完整命令“RAILS_ENV=production bundle exec rake assets:precompile”。 - Rajesh Omanakuttan
请检查答案的更新。我认为捆绑包缺少uglifier gem,请尝试一下。 - Rajesh Omanakuttan
我之前已经添加过了,不知道为什么它显示出错,尽管我已经安装了Uglifier。 - Green
只需使用“rake assets:precompile”命令,然后添加:assets组代码即可。 - Carlos Morales

6
你是否已经在你的Gemfile中添加了这个gem?
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

将那个宝石从资产组中移出,然后再次运行bundle,希望这能有所帮助!

我已经添加了 gem 'uglifier','>= 1.0.3'。 - Green
把那个宝石从资产组中移出去,我希望这会有所帮助! - Rajdeep Singh
这个问题可能会对你有所帮助 https://dev59.com/_WbWa4cB1Zd3GeqPSQPR - Rajdeep Singh
谢谢您的帮助,我昨天看到了这个问题,但它仍然存在同样的错误。 - Green

4
为了解释问题,您的错误如下所示:
LoadError: cannot load such file -- uglifier
      (in /home/cool_tech/cool_tech/app/assets/javascripts/application.js)

这意味着在你的应用程序中的某个地方,有一个引用 uglifier 的地方(很可能位于文件顶部的 manifest 区域内)。为了解决这个问题,你需要删除对 uglifier 的引用,或确保在你的应用程序中存在 uglifier 文件。因此,你得到的答案如下:

解决方法

如果你尝试将 gem 添加到 GemFile 中仍然无效,一个快速的解决方法是从你的 application.js manfiest 中移除对 uglifier 的任何引用。当然,这只是临时措施,但至少可以让你预编译你的资产。


我删除了 application.js 中的所有内容,但它仍然存在错误。 - Green
е“ҮпјҢеҘҪзҡ„ - дҪ жңүд»»дҪ•дҫқиө–дәҺuflifyзҡ„JSж–Ү件еҸӮиҖғеҗ—пјҹеҰӮжһңжІЎжңүпјҢдҪ жңүд»»дҪ•дјҡи°ғз”Ёе®ғзҡ„gemsеҗ—пјҹжңүд»»дҪ•JSдёӯеҝғзҡ„gemsеҗ—пјҹ - Richard Peck
老实说,我不知道 uflify 用来做什么,所以我确定我的 .js 文件没有引用 uflify。但我不知道其他 gem 是否会调用它。 - Green
你能把你的项目放到Github仓库并与我们分享吗?这将有助于我们诊断您可能遇到的任何潜在问题。 - Richard Peck
由于这是我的公司项目,我不能做。谢谢你的帮助 :) - Green

0

我发现如果在没有进行bundle update的情况下预编译,我的备份项目可以正常工作。也许是由于gem更新出了问题,但我不知道哪个gem出了错。


0
今天我遇到了这个问题。我通过明确我的需求来解决它。
gem 'uglifier', '>= 1.0.3', require: 'uglifier'

我的还在资源组里。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接