LoadError:无法加载“听”gem(Rails 5)

Posted

技术标签:

【中文标题】LoadError:无法加载“听”gem(Rails 5)【英文标题】:LoadError: Could not load the 'listen' gem (Rails 5) 【发布时间】:2016-12-04 11:01:48 【问题描述】:

我有一个 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)

我已验证 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, < 4.0)
  listen (~> 3.1.5)
  spring-watcher-listen (~> 2.0.0)

我已经更新了捆绑包,安装了捆绑包,并验证了 gem install listen 是否有效。这在本周早些时候工作,但我没有运气回过头来完成我的提交。

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

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

$ bundle package | grep 'listen'

感谢您的帮助!

更新:

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

【问题讨论】:

我遇到了这个问题并通过运行bundle install --with development test 解决了它。 生产服务器不需要开发和测试 gem。所以在bundle install --without development test 之后,您可以通过识别环境来运行rails 命令。 RAILS_ENV=production bundle exec rails sbundle exec rails s -e production 【参考方案1】:

如果您在 Rails 5 上并且使用默认的 config/environments/development.rb 文件,它将在其中包含这行代码。

config.file_watcher = ActiveSupport::EventedFileUpdateChecker

这需要 gem 监听。当我将 rails 4 升级到 rails 5 时,这让我有点失望

编辑: 忘了提一下,如果你注释掉那行代码,它就不再需要监听 gem。

【讨论】:

谢谢。我需要注释掉那一行。这是一种静默依赖,因为它不会在 bundler 中显示为依赖。 是的,将其注释掉会使上面的错误消失。忘了说那部分了。 我没有将其注释掉,而是以if defined? Listen 为条件。我宁愿不描述为什么这是必要的原因...... 这对我来说很关键——理解听(和 spring 和 spring-watcher-listen)在开发中是可选的。我对上述 development.rb 进行了更改,并从 Gemfile 中注释掉了这 3 个 gem,重新运行 bundle install,然后瞧!我的 Rails 应用程序将再次在本地运行! :D 我会鼓励在这里使用 listen gem,因为它应该可以更有效地检测开发中的代码更改。【参考方案2】:

您可能在某个时候错误地设置了bundle install --without,我确实这样做了。

要恢复此运行:

bundle config --delete without

我也错误地手动设置了with 选项时也运行了bundle config --delete with。运行两者应该可以让您恢复默认行为。

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


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

Gems in the groups development and test were not installed.

上面的解决方案肯定对你有用。

【讨论】:

bundle config --delete without 为我解决了这个问题。谢谢 为什么要在生产机器上安装测试/开发 gem?而是在显式生产模式下运行命令。 运行bundle install 的建议很有启发性。在本地,该命令未在此应用程序上运行(但可能在其他应用程序上),并且 bundler 和 rails 运行顺利。正是在以开发模式部署时,才出现此问题。那么cap 命令是否适用于所有应用程序?【参考方案3】:

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

错误:

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

在此之后,使用该代码:

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

终于

bundle install

【讨论】:

就像一只猴子驾驶 747 一样,我输入了这些命令,它就像一个魅力!克里斯。 @BKSpureon 这对我不起作用(另一个答案确实如此),但赞成关于猴子驾驶 747 的搞笑评论:D 这应该被标记为问题的正确答案【参考方案4】:

我将此作为答案发布,但我不喜欢它。

我可以通过将gem 'listen', '~&gt; 3.1.5' 放入全局 Gemfile(并将其从:development 中删除)来“修复”该问题。然后所有错误都消失了,一切正常,但这似乎是错误的。

【讨论】:

现在有同样的问题,有什么更新吗? @PetrosKyriakou 不幸的是没有。希望发布的解决方案同时对您有用。 是的,它确实做到了,但我不喜欢这个解决方案比你更......无论如何感谢它! 嗨,我想知道是否可以完全禁用监听 gem?特别是在生产中? 我今天遇到了同样的错误,我检查了我的怀疑,结果我是对的。我会发布一个可能的答案。【参考方案5】:

我在运行rails c 时遇到了同样的问题。

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

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

    在 ~/.bash_profile 中添加 export RAILS_ENV=production 明确编写我希望命令执行的环境,例如 bundle exec rake a_rake:task RAILS_ENV=production rails console --env=production 等...

【讨论】:

我遇到了同样的问题,因为我使用bundle install --without development test 安装我的应用程序,然后在没有指定RAILS_ENV=production 的情况下运行它。所以我的应用程序在没有安装开发 gem 的情况下以 development 模式运行。 这是在我由 capistrano 部署的实时应用程序中运行 rails c 所必需的。【参考方案6】:

从 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

这会预编译没有问题的资产,并且修复了“缺少资产”问题。

【讨论】:

【参考方案7】:

经过多次重建试验后,我找到了一个简单的修复方法

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

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

【讨论】:

【参考方案8】:

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

回顾一下,安装了 production 模式的依赖项,而 rails 由于忘记设置 RAILS_ENV=production 而试图以 development 模式启动。如果我继续将 gem listen 添加到 production 依赖项中,我将在开发模式下运行而不会注意到。

对我来说,解决方案是在执行任何 rails 命令之前执行export RAILS_ENV=production 并保持依赖关系完整。希望我能解释一下。

【讨论】:

【参考方案9】:

我遇到了同样的问题,我通过运行解决它

rails c -e production

【讨论】:

解决方案不能解释问题的本质。在生产模式下,不再从 Gemfile 安装 listen gem。因此,生产切换完全绕过了要求。 @Kunthar 制作不需要监听 gem。由于代码被冻结,没有理由监听代码修改。那里不需要任何开发和测试宝石。除非你在服务器上编码;)【参考方案10】:

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

但我在ruby 2.5.1 上创建了rails new project。您的ruby2.2。所以因果关系在我的情况下可能会有所不同。

当我使用rails new project 时,它是在5.2 版本中创建的,然后我将gem 更改为5.1.2 并出现问题。当我在 Gemfile 中使用 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

这种方式“听”在 gemfile 中自动显示给我

【讨论】:

【参考方案11】:

我正在使用 Docker,执行时遇到此错误

rails webpacker:install

发生这种情况是因为该行之前正在执行

ENV RAILS_ENV production
RUN chmod +x bin/rails
ENTRYPOINT ["bin/rails"]

通过简单地在此之后放置所有 rails 命令来修复它

【讨论】:

以上是关于LoadError:无法加载“听”gem(Rails 5)的主要内容,如果未能解决你的问题,请参考以下文章

机架错误 - LoadError:无法加载此类文件

Ruby:无法加载此类文件 - LoadError

LoadError: 无法加载此类文件 -- test/unit/error

rails 无法加载此类文件 -- rake (LoadError)

Ruby loaderror 无法加载此类文件

LoadError: 无法加载此类文件 -- 目录/文件名