LoadError: Could not load the 'listen' gem

2020-02-08 07:01发布

I've got an API mode Rails 5 app that won't let me run rake routes or rails s. The error I get is:

$ 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 listen is in the development group in my 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:

$ 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 listen works. This was working earlier this week, but I'm not having luck going back through my commits.

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

I don't see it in vendor/cache but I'm not sure what to make of that...

$ 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.

9条回答
放荡不羁爱自由
2楼-- · 2020-02-08 07:09

I had the same issue. Thanks to @newdark answer I figured out the correct solution. Basically I wanted to deploy rails in production mode. But obviously forgot to set environment variable RAILS_ENV=production before running server.

So to recap, dependencies for production mode were installed while rails tried to start in development mode due to forgetting to set RAILS_ENV=production. If I went on to add gem listen to the production dependencies, I'd be running in development mode without being able to notice.

For me the solution was to do export RAILS_ENV=production before executing any rails commands and keep dependencies intact. Hope I managed to explain.

查看更多
你好瞎i
3楼-- · 2020-02-08 07:15

If you are on rails 5 and you are using the default config/environments/development.rb file it will have this line of code in there.

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

edit: Forgot to mention that if you comment that line of code out it will not need the listen gem anymore.

查看更多
女痞
4楼-- · 2020-02-08 07:20

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:

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.

查看更多
男人必须洒脱
5楼-- · 2020-02-08 07:21

I'm having the same problem by running rails c.

By reading this other Stack Overflow post I did realize that it is normal that both bundle exec rake command or rails console are running in a default production environment.

I figured I will solve the issue either by:

  1. adding export RAILS_ENV=production in ~/.bash_profile
  2. explicitly writing the environment in which I want the command to execute like bundle exec rake a_rake:task RAILS_ENV=production rails console --env=production etc...
查看更多
爷的心禁止访问
6楼-- · 2020-02-08 07:23

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.

查看更多
\"骚年 ilove
7楼-- · 2020-02-08 07:24

I had the same problem, i fix it by running

rails c -e production
查看更多
登录 后发表回答