LoadError:无法加载’listen’gem(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 `' LoadError: cannot load such file -- listen .../config/environment.rb:5:in `' Tasks: TOP => routes => environment (See full trace by running task with --trace) 

我已经validation了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 

而且它在我的Gemfile.lock中:

 $ cat Gemfile.lock | grep 'listen' listen (3.1.5) spring-watcher-listen (2.0.0) listen (>= 2.7,  3.1.5) spring-watcher-listen (~> 2.0.0) 

我已经更新了捆绑包,并安装了捆绑包,并validation了gem install listen工作原理。 本周早些时候这个工作正在进行中,但我通过我的提交没有运气。

 $ ruby -v ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14] 

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

 $ bundle package | grep 'listen' 

感谢帮助!

更新:

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

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

 config.file_watcher = ActiveSupport::EventedFileUpdateChecker 

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

编辑:忘了如果你提到如果你评论那行代码,它将不再需要listen gem

您可能错误地设置了bundle install --without在某些时候没有,我确实无论如何都做了。

要恢复此运行:

bundle config --delete without

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

删除without配置后,我可以再次成功运行bundle install ,然后我的rails srails db:migrate等工作。


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

Gems in the groups development and test were not installed.

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

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

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

我通过运行rails c遇到了同样的问题。

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

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

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

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

 ActionView::Template::Error (The asset "asterisk_orange.png" is not present in the asset pipeline.) 

尝试预编译资产会显示“gem listen error”:

 $ bundle exec rake assets:precompile rake aborted! LoadError: Could not load the 'listen' gem. Add `gem 'listen'` to the development group of your Gemfile 

我的解决方案是明确设置生产环境:

 $ RAILS_ENV=production bundle exec rake assets:precompile 

这预先编译了没有问题的资产,并修复了“遗失资产”问题。

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

因此,回顾一下,由于忘记设置RAILS_ENV=production ,rails尝试在development模式下启动production模式的依赖关系。 如果我继续添加gem listen production依赖项,我将在开发模式下运行而无法注意到。

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

我用过这个: bundle install --without development

错误:

无法加载’listen’gem。 将gem 'listen'添加到Gemfile的开发组(LoadError)

在此之后,使用该代码:

 bundle config --delete without bundle config --delete with 

最后

 bundle install 

我有同样的问题,我通过运行修复它

 rails c -e production 

我在尝试生成mongoid database file时遇到了同样的错误。

但我在ruby 2.5.1上创建了rails new project 。 你的ruby2.2 。 因此我的情感因果关系可能不同。

当我使用rails new project ,它是在5.2版本中创建的,然后我将gem更改为5.1.2并出现问题。 当我使用rails _5.1.6_ new中的rails _5.1.6_ new创建时,会生成额外的开发包。

  group :development do # Access an IRB console on exception pages or by using <%= console %> anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # 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 

这样’listen’在gemfile中自动显示我