LoadError:无法加载'listen'gem(Rails 5)

los*_*her 43 ruby-on-rails listen ruby-on-rails-5

我有一个API模式Rails 5应用程序,不会让我运行rake routesrails s.我得到的错误是:

$ rake routes
rake aborted!
LoadError: Could not load the 'listen' gem. Add `gem 'listen'` to the development group of your Gemfile
.../config/environment.rb:5:in `<top (required)>'
LoadError: cannot load such file -- listen
.../config/environment.rb:5:in `<top (required)>'
Tasks: TOP => routes => environment
(See full trace by running task with --trace)
Run Code Online (Sandbox Code Playgroud)

我已经验证listen了我的Gemfile中的开发组:

group :development do
  gem 'listen', '~> 3.1.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end
Run Code Online (Sandbox Code Playgroud)

而且它在我的Gemfile.lock中:

$ cat Gemfile.lock | grep 'listen'
    listen (3.1.5)
    spring-watcher-listen (2.0.0)
      listen (>= 2.7, < 4.0)
  listen (~> 3.1.5)
  spring-watcher-listen (~> 2.0.0)
Run Code Online (Sandbox Code Playgroud)

我已经更新了捆绑包,并且已经安装了捆绑包,并验证了它是否gem install listen有效 本周早些时候这个工作正在进行中,但是通过我的提交我没有运气.

$ ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
Run Code Online (Sandbox Code Playgroud)

我没有看到它,vendor/cache但我不知道该怎么做...

$ bundle package | grep 'listen'
Run Code Online (Sandbox Code Playgroud)

感谢帮助!

更新:

我可以通过放入gem 'listen', '~> 3.1.5'全局Gemfile(并从中删除:development)来"修复"问题.然后所有错误消失,一切正常,但这似乎是错误的.

bra*_*-it 65

如果您使用的是rails 5,并且您使用的是默认的config/environments/development.rb文件,那么它将包含这行代码.

config.file_watcher = ActiveSupport::EventedFileUpdateChecker
Run Code Online (Sandbox Code Playgroud)

这需要宝石听.这让我有点兴奋,因为我正在对轨道5进行轨道4升级

编辑:如果你提到如果你注释掉那行代码就忘了就不再需要了

  • 谢谢.我需要评论这条线.这是一种静默依赖,因为它不会在bundler中显示为依赖. (3认同)

mtr*_*lle 45

你可能错误地设置bundle install --without了某些点,我肯定也做了.

要恢复此运行:

bundle config --delete without

我也跑了,bundle config --delete with因为我手动设置with选项也是错误的.运行这两个应该会让您回到默认行为.

在已经删除的without配置我可以成功运行bundle install一次后我的rails s,rails db:migrate等工作.


您可以通过运行确认这是否是您的问题,bundle install并查看输出中的倒数第二行.如果它指出:

Gems in the groups development and test were not installed.

这肯定是上面的解决方案应该适合你.


los*_*her 16

我发布这个作为答案,但我不喜欢它.

我可以通过放入gem 'listen', '~> 3.1.5'全局Gemfile(并从中删除:development)来"修复"问题.然后所有错误消失,一切正常,但这似乎是错误的.


小智 9

我用这个: bundle install --without development

错误:

无法加载“听” gem。添加gem 'listen'到您的Gemfile的开发组中(LoadError)

之后,使用该代码:

bundle config --delete without
bundle config --delete with
Run Code Online (Sandbox Code Playgroud)

最后

bundle install
Run Code Online (Sandbox Code Playgroud)

  • 就像一只猴子飞过747一样,我输入了这些命令,它就像一个吊饰一样起作用!chrs。 (4认同)

小智 7

从Rails 5.1.5升级到5.2.0之后,今天我遇到了类似的问题。首次运行服务器时,出现以下“资产丢失”问题:

ActionView::Template::Error (The asset "asterisk_orange.png" is not present in the asset pipeline.)
Run Code Online (Sandbox Code Playgroud)

尝试预编译资产会显示“宝石监听错误”:

$ bundle exec rake assets:precompile
rake aborted!
LoadError: Could not load the 'listen' gem. Add `gem 'listen'` to the development group of your Gemfile
Run Code Online (Sandbox Code Playgroud)

我的解决方案是显式设置生产环境:

$ RAILS_ENV=production bundle exec rake assets:precompile
Run Code Online (Sandbox Code Playgroud)

这可以预编译没有问题的资产,并且“丢失资产”问题已得到解决。


小智 7

经过大量重建试验后,我找到了一个简单的解决方法

bundle config --delete without
bundle config --delete with
bundle install
Run Code Online (Sandbox Code Playgroud)

如果您在正确设置环境之前不小心构建了 gemset,那么这可能有助于解决问题。


Fra*_*eli 6

我跑步时遇到同样的问题rails c.

通过阅读其他Stack Overflow 帖子,我意识到这两个bundle exec rake命令或rails console在默认production环境中运行是正常的.

我想我会通过以下方式解决问题:

  1. 添加export RAILS_ENV=production〜/ .bash_profile
  2. 显式编写我希望命令执行的环境,如bundle exec rake a_rake:task RAILS_ENV=production rails console --env=production...


ako*_*nov 5

我遇到过同样的问题。感谢@newdark 的回答,我找到了正确的解决方案。基本上我想在production模式下部署rails 。但显然忘记设置环境变量RAILS_ENV=production在运行服务器之前。

所以回顾一下,production当 railsdevelopment由于忘记设置RAILS_ENV=production. 如果我继续将 gem 添加listenproduction依赖关系,我会在发展模式,不能够通知运行。

对我来说,解决方案是export RAILS_ENV=production在执行任何 rails 命令之前执行并保持依赖关系不变。希望我设法解释。