1. ホーム
  2. javascript

[解決済み] Windowsでrubyチュートリアルに従おうとするとExecJS::RuntimeErrorが発生する

2023-01-03 21:56:07

質問

UPDATEです。 コリン の提案で、 //= require_tree .の行を削除したところ、問題が修正されました。

私は、そこにあるすべての提案に従って、私の問題を解決しようとして、2 日以上無駄にしました。私は http://ruby.railstutorial.org の本を Windows マシンで読もうとしているのですが、次のような厄介なエラーが発生して、どうにもなりません。

ExecJS::RuntimeError in Static_pages#home

Showing C:/Users/.../bootcamp-sample-app/app/views/layouts/application.html.erb where line #6 raised:

["ok","(function() {\n\n\n\n}).call(this);\n"]
(in C:/Users/.../bootcamp-sample-app/app/assets/javascripts/sessions.js.coffee)
Extracted source (around line #6):

3: <head>
4:   <title><%= full_title(yield(:title)) %></title>
5:   <%= stylesheet_link_tag    "application", media: "all" %>
6:   <%= javascript_include_tag "application" %>
7:   <%= csrf_meta_tags %>
8:   <%= render 'layouts/shim' %>
9: </head>
Rails.root: C:/Users/.../bootcamp-sample-app

Application Trace | Framework Trace | Full Trace
app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb___487732698_30422172'
Request

nodejsをmsiでインストールしたり、execjs 1.3.0を使ったり、もう思い出せないくらい他のことも含めて、あらゆる提案を試しました。以下は gemファイルです。

source 'https://rubygems.org'

gem 'rails', '3.2.8'
gem 'bootstrap-sass', '2.0.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.0.1'
gem 'will_paginate', '3.0.3'
gem 'bootstrap-will_paginate', '0.0.6'

group :development, :test do
  gem 'sqlite3', '1.3.5'
  gem 'rspec-rails', '2.10.0'
  gem 'guard-rspec', '0.5.5'
  gem 'guard-cucumber'
end

group :development do
  gem 'annotate', '2.5.0'
end


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails'
  gem 'coffee-rails'
  gem 'coffee-script'
  gem 'uglifier'
end

gem 'jquery-rails', '2.0.2'

gem 'execjs'

# Gems on Linus/Mac
#gem 'therubyracer'


group :test do
  gem 'capybara', '1.1.2'
  gem 'guard-spork', '0.3.2'
  gem 'spork', '0.9.0'
  gem 'factory_girl_rails', '1.4.0'
  gem 'cucumber-rails', '1.2.1', require: false
  gem 'database_cleaner', '0.7.0'


# Test gems on Linux
#  gem 'rb-inotify', '0.8.8'
#  gem 'libnotify', '0.5.9'

# Test gems on Macintosh OS X
#  gem 'selenium-webdriver', '~> 2.22.0'
#  gem 'rb-fsevent', '0.9.1', :require => false
#  gem 'growl', '1.0.3'

# Test gems on Windows
# gem 'rb-fchange', '0.0.5'
# gem 'rb-notifu', '0.0.4'
# gem 'win32console', '1.3.0'
end

group :production do
#  gem 'therubyracer'
  gem 'pg', '0.12.2'
end

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
#gem 'debugger''

で、以下は sessions.js.coffee

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

アプリケーション.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 vendor/assets/javascripts of plugins, if any, 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
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require bootstrap

application.html.erb

<!DOCTYPE html>
 <html>
 <head>
   <title><%= full_title(yield(:title)) %></title>
   <%= stylesheet_link_tag    "application", media: "all" %>
   <%= javascript_include_tag "application" %>
   <%= csrf_meta_tags %>
   <%= render 'layouts/shim' %>
 </head>
 <body>
 <%= render 'layouts/header' %>
 <div class="container">
   <%= yield %>
   <%= render 'layouts/footer' %>
 </div>
 </body>
 </html>

以下は コンソールコンテンツ

Processing by StaticPagesController#home as HTML
  Rendered static_pages/home.html.erb within layouts/application (45.0ms)
Completed 500 Internal Server Error in 1136ms

ActionView::Template::Error (["ok","(function() {\n\n\n\n}).call(this);\n"]
  (in C:/Users/.../bootcamp-sample-app/app/assets/javascripts/sessions.js.coffee)):
    3: <head>
    4:   <title><%= full_title(yield(:title)) %></title>
    5:   <%= stylesheet_link_tag    "application", media: "all" %>
    6:   <%= javascript_include_tag "application" %>
    7:   <%= csrf_meta_tags %>
    8:   <%= render 'layouts/shim' %>
    9: </head>
  app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb___487732698_30422172'


  Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.0ms)
  Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
  Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (34.0ms)

Devkitをインストールし、様々なgemsを試しましたが、Windowsでの開発を助けることができるような変更を提案してください。私はすべてのためにrubyinstallerを使用しました。

何が足りないのでしょうか?

どのように解決するのですか?

私の友人は、数か月前に Win 8 RTM で Rails のチュートリアルを試みていて、このエラーに遭遇しました。 この問題が Windows 7 にも存在するかどうかは分かりませんが、これが役に立つかもしれません。


オプションです。

1) 削除 //= require_tree . / 問題の無視 ColinRが上で述べたように、この行はそもそも問題を引き起こすべきではないのです。 ExecJSがシステム上のJavaScriptランタイムで適切に動作するためには実際の問題があり、この行を削除することはその事実を無視することになります。

2) Node.jsをインストールする / 実行する-。 多くの人は、Node.jsをインストールして、すでにシステム上にあるJavaScriptランタイムの代わりにそれを使うことにしているようです。 それは有効な選択肢ですが、追加のソフトウェアを必要とし、ExecJSがシステム上のJavaScriptランタイムと正しく動作しないという本来の問題を回避しているにすぎません。 もし、あなたのシステム上の既存のJavaScriptランタイムが動作することになっているのなら、なぜ、さらにソフトウェアをインストールする代わりに、それを動作させないのでしょうか? ExecJS の作成者によると、Windows にすでに組み込まれているランタイムは実際にサポートされているそうです...。

ExecJS は、Ruby から JavaScript コードを実行することができます。それは、JavaScript プログラムを評価するために利用可能な最良のランタイムを自動的に選択し、その結果を Ruby オブジェクトとして返します。

ExecJS はこれらのランタイムをサポートしています。

  • therubyracer - Rubyに埋め込まれたGoogle V8
  • therubyrhino - JRubyに組み込まれたMozilla Rhino
  • Node.js
  • Apple JavaScriptCore - Mac OS X に含まれています。
  • Microsoft Windows Script Host (JScript)

(github.com/sstephenson/execjs#execjs より)

3) 実際に問題を解決する / 学習する -。 選択肢 1 と 2 の知識を使って、他の解決策を探します。 私たちが抱えていた根本的な問題についての情報を実際に見つける前に、選択肢 1 または 2 が認められた解決策であるのを見て、どれだけの Web ページを閉じたかわかりません。 探し続けたのは、Railsチームが (1) scaffoldで生成されたすべてのプロジェクトに問題を引き起こすコードを挿入したり、(2) そのデフォルトのコード行を実行するためだけに追加のソフトウェアをインストールするよう要求したりするとは思えなかったからです。 そして、最終的に私たちは根本的な問題の修正にたどり着きました (マイルは異なるかもしれません)。



私たちのために機能した修正。 問題のあるシステムで、ExecJSの runtimes.rb ファイルを見つけてください。 それは以下のようになります。 これ . 見つかったファイルをバックアップのためにコピーしておきます。元のruntimes.rbを編集のために開く。 以下の行で始まるセクションを探します。 JScript = ExternalRuntime.new( . そのセクションで、以下の行を含む :command => "cscript //E:jscript //Nologo //U", - を削除します。 //U だけです。 そして、その行には :encoding => 'UTF-16LE' # CScript with //U returns UTF-16LE - をUTF-16LEに変更します。 UTF-8 . 変更をファイルに保存します。 ファイルのこのセクションは、これで読めるようになります。

JScript = ExternalRuntime.new(
    :name        => "JScript",
    :command     => "cscript //E:jscript //Nologo",
    :runner_path => ExecJS.root + "/support/jscript_runner.js",
    :encoding    => 'UTF-8' # CScript with //U returns UTF-16LE
)

次に、Railsサーバを停止して再起動し、ブラウザで元のエラーが発生したページを更新してください。うまくいけば、エラーなしでページが読み込まれます。 私たちが最初に結果を投稿した ExecJS issue スレッドはこちらです。 https://github.com/sstephenson/execjs/issues/81#issuecomment-9892952

これで問題が解決しなかった場合は、いつでも変更した runtimes.rb を (できれば) 作成したバックアップ コピーで上書きすれば、すべてが振り出しに戻るでしょう。 その場合は、オプション 3 を検討し、検索を続けてください。 require_treeの削除やnode.jsのインストールでなければ、すでにたくさん出回っています。)