Git Bash中启动Rails Server报错求助:缺少JavaScript运行时
解决Rails启动时Uglifier找不到JavaScript运行时的问题
嘿,我之前也踩过这个坑!别慌,咱们一步步来排查解决:
第一步:确认Node.js是否真的能被终端识别
你说已经装了Node.js,但有时候安装后终端(比如你用的Git Bash)没及时读取新的环境变量,导致Rails找不到它。先打开Git Bash输入:
node -v
如果能输出类似v18.17.0这样的版本号,说明Node是正常可用的;如果提示command not found,那你得把Node的安装路径加到系统的环境变量PATH里,然后重启Git Bash再试。
第二步:调整Gemfile并重新安装依赖
你取消了# gem 'therubyracer', platforms: :ruby的注释,但其实有Node.js的话,therubyracer完全没必要装——它是Ruby实现的JS运行时,和Node功能重复,反而可能导致冲突。建议你把这行重新注释掉,然后在项目根目录运行:
bundle install
确保依赖安装正确。
第三步:强制指定ExecJS使用Node.js
如果上面两步都试过还是不行,咱们可以手动告诉ExecJS用Node作为运行时:
在你的Rails项目里创建config/initializers/execjs.rb文件,写入以下内容:
ExecJS.runtime = ExecJS::Runtimes::Node
保存后再运行rails s试试,应该就能正常启动了。
你的报错详情:
$ rails s c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:85:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'uglifier'. (Bundler::GemRequireError) Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. Backtrace for gem load error is: c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/execjs-2.7.0/lib/execjs.rb:4:in `<top (required)>' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/uglifier-4.1.10/lib/uglifier.rb:5:in `require' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/uglifier-4.1.10/lib/uglifier.rb:5:in `<top (required)>' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:82:in `require' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:82:in `block (2 levels) in require' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:77:in `each' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:77:in `block in require' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:66:in `each' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:66:in `require' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler.rb:108:in `require' c:/Sites/myrubyblog/config/application.rb:7:in `<top (required)>' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:133:in `require' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:133:in `block in perform' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:130:in `tap' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:130:in `perform' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/command/base.rb:63:in `perform' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/command.rb:44:in `invoke' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands.rb:16:in `<top (required)>' bin/rails:4:in `require' bin/rails:4:in `<main>' Bundler Error Backtrace: from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:81:in `block (2 levels) in require' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:77:in `each' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:77:in `block in require' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:66:in `each' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:66:in `require' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler.rb:108:in `require' from c:/Sites/myrubyblog/config/application.rb:7:in `<top (required)>' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:133:in `require' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:133:in `block in perform' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:130:in `tap' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:130:in `perform'
内容的提问来源于stack exchange,提问作者user8268874
相关产品推荐
相关产品推荐

