簡體   English   中英

加載錯誤:無法加載“監聽”gem(Rails 5)

[英]LoadError: Could not load the '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 `<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)

我已經更新了 bundle,並安裝了 bundle,並驗證了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', '~> 3.1.5'放在全局 Gemfile 中來“修復”問題(並將其從:development刪除)。 然后所有錯誤都消失了,一切正常,但這似乎是錯誤的。

如果您使用的是 rails 5 並且您使用的是默認的config/environments/development.rb文件,它將在其中包含這行代碼。

config.file_watcher = ActiveSupport::EventedFileUpdateChecker

這需要 gem 聆聽。 這讓我感到有些困惑,因為我正在將 Rails 4 升級到 Rails 5

編輯:忘了提及,如果您注釋掉該行代碼,它將不再需要監聽 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.

上面的解決方案肯定對你有用。

我用過這個: bundle install --without development

錯誤:

無法加載“聽”寶石。 gem 'listen'添加到 Gemfile 的開發組(LoadError)

在此之后,使用該代碼:

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

最后

bundle install

我將此作為答案發布,但我不喜歡它。

我可以通過將gem 'listen', '~> 3.1.5'放在全局 Gemfile 中來“修復”問題(並將其從:development刪除)。 然后所有錯誤都消失了,一切正常,但這似乎是錯誤的。

我在運行rails c遇到了同樣的問題。

通過閱讀其他 Stack Overflow 帖子,我確實意識到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等...

從 Rails 5.1.5 升級到 5.2.0 后,我今天遇到了類似的問題。 第一次運行服務器時出現以下“丟失資產”問題:

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

嘗試預編譯資產顯示“寶石監聽錯誤”:

$ 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

這會預編譯沒有問題的資產,並且修復了“丟失資產”的問題。

經過大量重建試驗后,我找到了一個簡單的解決方法

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

如果您在正確設置環境之前不小心構建了 gemset,那么這可能有助於解決問題。

我有同樣的問題。 感謝@newdark 的回答,我找到了正確的解決方案。 基本上我想在production模式下部署 rails。 但顯然忘記在運行服務器之前設置環境變量RAILS_ENV=production

所以回顧一下,由於忘記設置RAILS_ENV=production當 rails 嘗試以development模式啟動時,安裝了production模式的依賴項。 如果我繼續添加 gem listen production依賴項,我將在開發模式下運行而無法注意到。

對我來說,解決方案是在執行任何 rails 命令之前執行export RAILS_ENV=production並保持依賴關系不變。 希望我設法解釋。

我遇到了同樣的問題,我通過運行來解決它

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

這樣“聽”會自動在 gemfile 中顯示我

我在使用 Docker 並在執行時遇到此錯誤

rails webpacker:install

發生這種情況是因為此行之前已被執行

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

通過簡單地在此之后放置所有 rails 命令來修復它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM