部署到Heroku出现的问题:ExecJS :: RuntimeError:SyntaxError:意外的标记

3

我在Stackoverflow上看到了一个类似的 post,出现了相似的错误,但据我所知,那个问题还没有被解决,而且它和我的问题之间似乎存在一些差异。

当然,本地一切都很完美。但是当我将其推送到Heroku时,发生了这种疯狂的情况:

remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote:        Running: rake assets:precompile
remote:        rake aborted!
remote:        ExecJS::RuntimeError: SyntaxError: Unexpected token punc «{», expected punc «(»
remote:        JS_Parse_Error.Object.defineProperty.get ((execjs):3538:621)
remote:        (execjs):4060:47
remote:        (execjs):1:102
remote:        Object.<anonymous> ((execjs):1:120)
remote:        Module._compile (module.js:456:26)
remote:        Object.Module._extensions..js (module.js:474:10)
remote:        Module.load (module.js:356:32)
remote:        Function.Module._load (module.js:312:12)
remote:        Function.Module.runMain (module.js:497:10)
remote:        startup (node.js:119:16)
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/execjs-2.7.0/lib/execjs/external_runtime.rb:39:in `exec'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/execjs-2.7.0/lib/execjs/external_runtime.rb:21:in `eval'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/execjs-2.7.0/lib/execjs/external_runtime.rb:46:in `call'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/uglifier-3.1.6/lib/uglifier.rb:190:in `run_uglifyjs'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/uglifier-3.1.6/lib/uglifier.rb:152:in `compile'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/uglifier_compressor.rb:53:in `call'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/uglifier_compressor.rb:28:in `call'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:75:in `call_processor'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:56:in `reverse_each'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:56:in `call_processors'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/loader.rb:134:in `load_from_unloaded'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/loader.rb:60:in `block in load'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/loader.rb:44:in `load'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/cached_environment.rb:20:in `block in initialize'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/cached_environment.rb:47:in `yield'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/cached_environment.rb:47:in `load'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/base.rb:66:in `find_asset'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/base.rb:73:in `find_all_linked_assets'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:142:in `block in find'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:114:in `block (2 levels) in logical_paths'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:228:in `block in stat_tree'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:212:in `block in stat_directory'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:209:in `each'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:209:in `stat_directory'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:227:in `stat_tree'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:105:in `each'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:105:in `block in logical_paths'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:104:in `each'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:104:in `logical_paths'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:140:in `find'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:185:in `compile'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-rails-3.2.0/lib/sprockets/rails/task.rb:68:in `block (3 levels) in define'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.1/lib/rake/sprocketstask.rb:147:in `with_logger'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/sprockets-rails-3.2.0/lib/sprockets/rails/task.rb:67:in `block (2 levels) in define'
remote:        /tmp/build_c7ded3066a4d98b445e04f25befe0b17/vendor/bundle/ruby/2.2.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
remote:        Tasks: TOP => assets:precompile
remote:        (See full trace by running task with --trace)
remote:  !
remote:  !     Precompiling assets failed.
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote: 
remote:  !     Push failed

帖子中的问题一直存在,我对此一无所知。我的项目非常基础——它没有自定义的.js文件或其他任何东西。本质上,它是一个具有静态页面控制器的Rails应用程序。我甚至不知道从哪里开始查找问题。在一个类似的post上,有人提到他们删除了uglifier gem。但这对我来说并没有改变结果。

欢迎任何建议,但请理解我真的一无所知:我需要基本的逐步说明,就像我是一个4岁孩子一样。另外,我有一个由挫败感产生的愚蠢问题:为什么它不给我一个错误,明确指出我生成的代码中的某些特定部分,而不是所有这些看起来都是自动生成的垃圾?

提前致谢。

Gemfile:

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


gem 'rails', '~> 5.0.1'
gem 'bcrypt',         '3.1.11'
gem 'faker',          '1.6.6'
gem 'carrierwave', '~> 1.0'
gem 'mini_magick'
gem 'fog'
gem 'will_paginate',           '3.1.0'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'bootstrap-sass', '3.3.6'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'jquery-rails'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'puma'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'sqlite3'
  gem 'byebug', platform: :mri
end

group :development do
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.0.5'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  gem 'rails-controller-testing', '0.1.1'
  gem 'minitest-reporters',       '1.1.9'
  gem 'guard',                    '2.13.0'
  gem 'guard-minitest',           '2.4.4'
end

group :production do
  gem 'pg'
  gem 'rails_12factor', '0.0.2'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

application.js:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

你是否正在运行任何自定义的构建包?(我注意到这是来自Node启动文件的) - Bobby Matson
另外,如果您在本地运行 RAILS_ENV=production rake assets:precompile,是否能够重现此问题?如果可以,那么很可能是您的资产管道中出现了冲突。 - Bobby Matson
exec.js有什么问题吗?你在那里遇到了语法错误还是我误解了?它说期望而不是} - Fabrizio Bertoglio
@BobbyMatson,一开始我没有运行任何自定义构建包。另一个线程中的一个解决方案是运行一个可以在部署时进行清除的构建包。但这并没有解决问题...虽然问题解决后,这个构建包却产生了一个错误,所以我不得不将其删除。在尝试这个方法之前,将本地切换到构建生产环境-问题得到解决,但如果再次出现这种情况,我肯定会记住这个方法。 - vicmorrowshead
@FabrizioBertoglio exec.js,我认为是在预编译期间生成的 - 它实际上并不存在于我的系统中。似乎服务器的预编译正在这个文件中生成一些错误... - vicmorrowshead
1个回答

4
我最终解决了这个问题(至少本次是这样):
  1. 在本地预编译资源(rake assets:precompile)。这在本地系统上创建了两个文件,我认为它们是gz文件。
  2. 提交并推送到Heroku。这之后它就正常工作了。
尽管如此,我对此一无所知。我以前从未需要预编译资源。如果必须这样做,为什么本地主机总是能够正常工作而不执行此命令呢?而且,Heroku总是尝试预编译资源。当我推送时,为什么它成功了,推测是因为预先编译的资源?难道它不会重新进行预编译吗?
所以,目前为止,这个问题已经解决了。但是我不知道原因。

2
rake assets:precompile 只适用于开发环境,你需要使用 RAILS_ENV=production bundle exec rake assets:precompile 来进行生产环境的预编译。 你没有对生产环境进行预编译,而是对开发环境进行了预编译,然后将开发环境推送到了 Heroku 生产环境。有时候,旧的指纹资产不会随着你所做的修改而更新,因此你需要重新编译以生成那些带有指纹的 application-fingerpint.css 和 js 文件。 - Fabrizio Bertoglio

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