Ruby-on-rails 加载错误:无法加载“监听”gem(Rails 5)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38663706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LoadError: Could not load the 'listen' gem (Rails 5)
提问by lostphilosopher
I've got an API mode Rails 5 app that won't let me run rake routesor rails s. The error I get is:
我有一个 API 模式 Rails 5 应用程序,它不会让我运行rake routes或rails 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)
I've verified that listenis in the development group in my Gemfile:
我已经验证它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
And that it's in my Gemfile.lock:
它在我的 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)
I've bundle updated, and bundle installed, and verified that gem install listenworks. This was working earlier this week, but I'm not having luck going back through my commits.
我已经更新了捆绑包,安装了捆绑包,并验证了它的gem install listen工作原理。这在本周早些时候起作用了,但我没有运气回到我的提交中。
$ ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
I don't see it in vendor/cachebut I'm not sure what to make of that...
我没有看到它,vendor/cache但我不知道该怎么做...
$ bundle package | grep 'listen'
Appreciate the help!
感谢帮助!
Update:
更新:
I can "fix" the problem by putting gem 'listen', '~> 3.1.5'in the global Gemfile (and removing it from :development). Then all the errors go away and everything works, but that seems wrong.
我可以通过放入gem 'listen', '~> 3.1.5'全局 Gemfile(并将其从 中删除:development)来“修复”问题。然后所有错误都消失了,一切正常,但这似乎是错误的。
采纳答案by lostphilosopher
I'm posting this as an answer, but I don't like it.
我将此作为答案发布,但我不喜欢它。
I can "fix" the problem by putting gem 'listen', '~> 3.1.5'in the global Gemfile (and removing it from :development). Then all the errors go away and everything works, but that seems wrong.
我可以通过放入gem 'listen', '~> 3.1.5'全局 Gemfile(并将其从 中删除:development)来“修复”问题。然后所有错误都消失了,一切正常,但这似乎是错误的。
回答by newdark-it
If you are on rails 5 and you are using the default config/environments/development.rbfile it will have this line of code in there.
如果您使用的是 rails 5 并且您使用的是默认的config/environments/development.rb文件,它将在其中包含这行代码。
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
This requires the gem listen. This threw me for a bit as I was doing a rails 4 upgrades to a rails 5
这需要 gem 聆听。这让我感到有些困惑,因为我正在将 Rails 4 升级到 Rails 5
edit: Forgot to mention that if you comment that line of code out it will not need the listen gem anymore.
编辑:忘了提及,如果您注释掉该行代码,它将不再需要监听 gem。
回答by mtrolle
You might by mistake have set bundle install --withoutat some point, I sure did anyways.
你可能bundle install --without在某个时候错误地设置了,反正我确实这样做了。
To revert this run:
要恢复此运行:
bundle config --delete without
bundle config --delete without
I also ran bundle config --delete withas I manually set withoption as well by mistake. Running both should get you back to default behaviour.
我也跑了,bundle config --delete with因为我with也错误地手动设置了选项。运行两者应该让你回到默认行为。
After having deleted the withoutconfig I could successfully run a bundle installagain and afterwards my rails s, rails db:migrateetc. worked.
在已经删除的without配置我可以成功运行bundle install一次后我的rails s,rails db:migrate等工作。
You can confirm if this is your issue by running bundle installand look at the second last line in the output. If it states:
您可以通过运行bundle install并查看输出中的倒数第二行来确认这是否是您的问题。如果它说明:
Gems in the groups development and test were not installed.
Gems in the groups development and test were not installed.
It's for sure above solution should work for you.
上面的解决方案肯定对你有用。
回答by mix-fGt
I used this: bundle install --without development
我用过这个: bundle install --without development
Error:
错误:
Could not load the 'listen' gem. Add
gem 'listen'to the development group of your Gemfile (LoadError)
无法加载“听”宝石。添加
gem 'listen'到你的 Gemfile 的开发组(LoadError)
After this, use that code:
在此之后,使用该代码:
bundle config --delete without
bundle config --delete with
Finally
最后
bundle install
回答by Francesco Meli
I'm having the same problem by running rails c.
我在运行时遇到了同样的问题rails c。
By reading this other Stack Overflow postI did realize that it is normal that both bundle exec rakecommand or rails consoleare running in a default productionenvironment.
通过阅读其他 Stack Overflow帖子,我确实意识到bundle exec rake命令或rails console都在默认production环境中运行是正常的。
I figured I will solve the issue either by:
我想我会通过以下方式解决这个问题:
- adding
export RAILS_ENV=productionin ~/.bash_profile - explicitly writing the environment in which I want the command to execute like
bundle exec rake a_rake:task RAILS_ENV=productionrails console --env=productionetc...
- 添加
export RAILS_ENV=production~/.bash_profile - 明确地编写我希望命令在其中执行的环境,例如
bundle exec rake a_rake:task RAILS_ENV=productionrails console --env=production……
回答by Heiko Lübbe
I had similar problem today after upgrade from Rails 5.1.5 to 5.2.0. First time running the server there was the following 'missing assets' problem:
从 Rails 5.1.5 升级到 5.2.0 后,我今天遇到了类似的问题。第一次运行服务器时出现以下“丢失资产”问题:
ActionView::Template::Error (The asset "asterisk_orange.png" is not present in the asset pipeline.)
Trying to precompile the assets shows the '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
My solution was to explicit set production environment:
我的解决方案是显式设置生产环境:
$ RAILS_ENV=production bundle exec rake assets:precompile
This precompiles the assets w/o problems and the 'missing assets' problem was fixed.
这会预编译没有问题的资产,并且修复了“丢失资产”的问题。
回答by akostadinov
I had the same issue. Thanks to @newdark answer I figured out the correct solution. Basically I wanted to deploy rails in productionmode. But obviously forgot to set environment variable RAILS_ENV=productionbefore running server.
我遇到过同样的问题。感谢@newdark 的回答,我找到了正确的解决方案。基本上我想在production模式下部署rails 。但显然忘记RAILS_ENV=production在运行服务器之前设置环境变量。
So to recap, dependencies for productionmode were installed while rails tried to start in developmentmode due to forgetting to set RAILS_ENV=production. If I went on to add gem listento the productiondependencies, I'd be running in development mode without being able to notice.
所以回顾一下,production当 railsdevelopment由于忘记设置RAILS_ENV=production. 如果我去上创业板添加listen到production依赖关系,我会在发展模式,不能够通知运行。
For me the solution was to do export RAILS_ENV=productionbefore executing any rails commands and keep dependencies intact. Hope I managed to explain.
对我来说,解决方案是export RAILS_ENV=production在执行任何 rails 命令之前执行并保持依赖关系不变。希望我设法解释。
回答by Touseef Murtaza
I had the same problem, i fix it by running
我遇到了同样的问题,我通过运行来解决它
rails c -e production
回答by sonic
I had same error when trying to generate mongoid database file.
尝试生成mongoid database file.
but I created rails new projecton ruby 2.5.1. Your rubyis 2.2. so the causality could be different in my situtation.
但我rails new project在ruby 2.5.1. 你的ruby是2.2。所以在我的情况下因果关系可能会有所不同。
when I used rails new project, it was created in version 5.2, then I changed gem to 5.1.2 and problem arises. When I created with rails _5.1.6_ newin Gemfile there is generated additional pack for development.
当我使用rails new project它时,它是在5.2版本中创建的,然后我将gem更改为5.1.2并出现问题。当我rails _5.1.6_ new在 Gemfile 中创建时,会生成额外的开发包。
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
this way 'listen' showed me in gemfile automaticaly
这样“听”会自动在 gemfile 中显示我

