【问题标题】:Rails server does not start -> Could not find a JavaScript runtime [duplicate]Rails 服务器未启动-> 找不到 JavaScript 运行时 [重复]
【发布时间】:2013-05-30 21:21:52
【问题描述】:

我在文件夹 'issues' 中创建了一个新的 rails 应用程序,当我想要 'ruby s' 时出现错误。任何帮助将不胜感激

[thiago@netbox issues]$ rails s
/home/thiago/.rvm/gems/ruby-1.9.3-p429/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429/gems/execjs-1.4.0/lib/execjs.rb:5:in `<module:ExecJS>'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429/gems/execjs-1.4.0/lib/execjs.rb:4:in `<top (required)>'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `require'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `<top (required)>'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `require'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `<top (required)>'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `require'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `<top (required)>'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `require'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `block (2 levels) in require'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `each'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `block in require'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:59:in `each'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:59:in `require'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler.rb:132:in `require'
    from /home/thiago/Documents/wdi/rails_practice/issues/config/application.rb:7:in `<top (required)>'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/commands.rb:53:in `require'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/commands.rb:53:in `block in <top (required)>'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
    from /home/thiago/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

我将 'therubyracer' gem 添加到我的 Gemfile 中,然后 '$ bundle install' 工作!

Gemfile
source 'https://rubygems.org'

gem 'rails', '3.2.8'
gem 'sqlite3'

group :assets do
gem 'sass-rails',   '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'
gem 'therubyracer' # added

【问题讨论】:

  • 你能发布你的 Gemfile 的内容吗?

标签: ruby-on-rails ruby linux


【解决方案1】:

当我在一月份开始研究 Rails 时,我在 Windows 和 Ubuntu 上都遇到了同样的错误。错误非常简单。您需要一个 JavaScript 运行时环境来启动服务器。

对此有多种解决方案。我在 Ubuntu 上使用的是安装 NodeJS。

在 Ubuntu 上,您需要运行以下代码来安装 NodeJS:

sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get -y update
sudo apt-get -y install nodejs

这些是我用来执行此操作的命令,因为它将安装 NodeJS 的最新稳定版本,但您只需运行 sudo apt-get -y install nodejs 就可以了。

在 Windows 上,我使用的答案(可能还有其他答案)是编辑您的 Gemfile 以安装 therubyracer。我现在正在开发 Rails 4.0 RC1 应用程序,它们都在刚刚注释掉的 Gemfile 中具有 gem 'therubyracer', platform: :ruby

如果您在 Windows 上运行,您可以在 Gemfile 中添加 gem 'therubyracer',我敢打赌这会让您运行。然后运行bundle install

【讨论】:

  • 是的,therubyracer 工作了。
  • 太棒了。享受 Rails :)。
【解决方案2】:

找不到 JavaScript 运行时。我想你的 G​​emfile 中没有一个。 尝试编辑您的 Gemfile 并添加

gem 'therubyracer'

然后运行

$ bundle install

【讨论】:

    【解决方案3】:

    你需要安装一个javascript解释器:

    将此添加到您的 Gemfile:

    gem "libv8", ">= 3.11.8"
    gem "therubyracer", ">= 0.11.0", :group => :assets, :platform => :ruby, :require => "v8"
    

    您还可以检查其他选项,例如 Node.js 用于生产,但对于开发来说就足够了。

    【讨论】:

      【解决方案4】:

      您应该打开/home/thiago/.rvm/gems/ruby-1.9.3-p429/gems/execjs-1.4.0/lib/execjs/runtimes.rb 并转到51 行。查看 autodetect 正在寻找什么资源。这似乎不见了。

      您可能有一个未加载的依赖项。最好的策略是使用rbenvrvm 创建一个新的ruby gem 环境,然后再次捆绑。应该修复它。

      【讨论】:

        【解决方案5】:
        • 可能的问题:我的 Rails-server 没有在 Windows 7 上启动,因为 ExecJS 无法检测到 Javascript 运行时,即使我安装了 NodeJS。
        • 我的机器上的原因: NodeJS 目录是环境的一部分,但被引号包围(例如[...];"C:\Program Files\nodejs\";[...])。这导致where 和 ExecJS 无法理解路径条目,最终找不到 node.js 可执行文件。
        • 解决方案:见下文...

        为了让我的 Rails 服务器在 Windows(7 64 位)上启动,我

        • 安装 NodeJS 到一个包含空格的目录(例如在C:\development\nodejs\) ...因为如果相应的条目用引号引起来,则 where(Windows 版本)无法在 PATH 中找到可执行文件

        • 确保where node 返回了我刚刚安装的节点可执行文件(例如C:\development\nodejs\node.exe) (如果哪里找不到 NodeJS,execJS 可能也找不到) 如果 where node 返回错误,请检查您的 PATH 变量,您可能希望将 NodeJS 的条目移到前面并且不要使用引号

        • 通过将相应的行更改为从 Gemfile 中排除了 therubyracer gem 'therubyracer', :platforms =&gt; :ruby (如前所述)这将导致 ruby​​racer 安装在 Linux/生产环境中,而不是本地 Windows 环境中

        • 删除了 Gemfile.lock 并调用 bundle install 以便全新安装 gemfile

        • 使用 rails server 启动 Rails

        【讨论】:

          【解决方案6】:

          它不允许您在任何目录中启动服务器。只需转到根目录并键入

          rails server   
          

          rails s
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-03-02
            • 1970-01-01
            • 1970-01-01
            • 2013-03-22
            • 1970-01-01
            相关资源
            最近更新 更多