Rails: Why do I get "warning: already initialized constant JSON::VERSION" when running rake cucumber?
Asked Answered
C

3

22

I've just set up a Linux Mint box for rails development with rvm. I went ahead and generated a Rails 5 app, set up the mysql connection, added the cucumber-rails gem and then tried to run:

rake cucumber

For some reason, I was met with:

/usr/bin/ruby2.3 -S bundle exec cucumber --profile default
/usr/lib/ruby/vendor_ruby/json/version.rb:3: warning: already initialized constant JSON::VERSION
/var/lib/gems/2.3.0/gems/json-1.8.5/lib/json/version.rb:3: warning: previous definition of VERSION was here
/usr/lib/ruby/vendor_ruby/json/version.rb:4: warning: already initialized constant JSON::VERSION_ARRAY
/var/lib/gems/2.3.0/gems/json-1.8.5/lib/json/version.rb:4: warning: previous definition of VERSION_ARRAY was here
/usr/lib/ruby/vendor_ruby/json/version.rb:5: warning: already initialized constant JSON::VERSION_MAJOR
/var/lib/gems/2.3.0/gems/json-1.8.5/lib/json/version.rb:5: warning: previous definition of VERSION_MAJOR was here
/usr/lib/ruby/vendor_ruby/json/version.rb:6: warning: already initialized constant JSON::VERSION_MINOR
/var/lib/gems/2.3.0/gems/json-1.8.5/lib/json/version.rb:6: warning: previous definition of VERSION_MINOR was here
/usr/lib/ruby/vendor_ruby/json/version.rb:7: warning: already initialized constant JSON::VERSION_BUILD
/var/lib/gems/2.3.0/gems/json-1.8.5/lib/json/version.rb:7: warning: previous definition of VERSION_BUILD was here
/usr/lib/ruby/vendor_ruby/json/common.rb:99: warning: already initialized constant JSON::NaN
/var/lib/gems/2.3.0/gems/json-1.8.5/lib/json/common.rb:99: warning: previous definition of NaN was here
/usr/lib/ruby/vendor_ruby/json/common.rb:101: warning: already initialized constant JSON::Infinity
/var/lib/gems/2.3.0/gems/json-1.8.5/lib/json/common.rb:101: warning: previous definition of Infinity was here
/usr/lib/ruby/vendor_ruby/json/common.rb:103: warning: already initialized constant JSON::MinusInfinity
/var/lib/gems/2.3.0/gems/json-1.8.5/lib/json/common.rb:103: warning: previous definition of MinusInfinity was here
/usr/lib/ruby/vendor_ruby/json/common.rb:128: warning: already initialized constant JSON::UnparserError
/var/lib/gems/2.3.0/gems/json-1.8.5/lib/json/common.rb:128: warning: previous definition of UnparserError was here

This is, to my knowledge, a fairly plain vanilla installation, so I cannot understand what I've done to earn these warnings. Do I somehow have conflicting gems in place? If so, how do I resolve this?

Clupeoid answered 14/1, 2017 at 10:42 Comment(7)
Judging by the trace you are using globally installed ruby. Did you use rvm to install it globally?Fiden
@IvanKolmychek, I've got the ruby package installed and I subsequently installed rvm. Ruby is available globally, yes. Am I answering your question?Clupeoid
I get this issue too, but I don't have a solution. Doubt that globally installed Ruby is likely to be an issue - I'm using rbenv.Including
For the record, I did not ever find a solution, but rather proceeded with building a rails 4 app instead. I'll surely revisit one day-but not todayClupeoid
I am getting this same error, thoughts on how to fix this?Wherefore
I'm getting this issue too, only when running rspec tests using guard, Rails 5Rounds
Some gem in your Gemfile is depending on the json gem, but the json gem is probably also installed globally on your machine. Add the json gem to your Gemfile with bundle add json, run bundle install and these warnings should be gone.Makedamakefast
R
39

I managed to get rid of the JSON warnings by:

bundle clean --force
bundle
Rounds answered 14/4, 2017 at 22:38 Comment(1)
This command resolved my issues bundle clean --force && bundle update --forceIncursive
S
5

The json gem that came with your version of Ruby is a little outdated. If you want to get rid of these warnings, you have to include a more up to date version of it in your Gemfile:

gem 'json', '>= 2.0.0'

Then run bundle install.

Sebastien answered 5/4, 2017 at 12:15 Comment(0)
T
4

redshift5's answer worked for me. Just to add more context.

I had this same issue while working on an application with Rails 6.

I was getting warnings like this:

/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/2.7.0/json/version.rb:4: warning: already initialized constant JSON::VERSION
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/version.rb:4: warning: previous definition of VERSION was here
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/2.7.0/json/version.rb:5: warning: already initialized constant JSON::VERSION_ARRAY
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/version.rb:5: warning: previous definition of VERSION_ARRAY was here
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/2.7.0/json/version.rb:6: warning: already initialized constant JSON::VERSION_MAJOR
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/version.rb:6: warning: previous definition of VERSION_MAJOR was here
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/2.7.0/json/version.rb:7: warning: already initialized constant JSON::VERSION_MINOR
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/version.rb:7: warning: previous definition of VERSION_MINOR was here
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/2.7.0/json/version.rb:8: warning: already initialized constant JSON::VERSION_BUILD
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/version.rb:8: warning: previous definition of VERSION_BUILD was here
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/2.7.0/json/common.rb:100: warning: already initialized constant JSON::NaN
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:130: warning: previous definition of NaN was here
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/2.7.0/json/common.rb:102: warning: already initialized constant JSON::Infinity
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:132: warning: previous definition of Infinity was here
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/2.7.0/json/common.rb:104: warning: already initialized constant JSON::MinusInfinity
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:134: warning: previous definition of MinusInfinity was here
/home/promisechukwuenyem/.rbenv/versions/2.7.2/lib/ruby/2.7.0/json/common.rb:129: warning: already initialized constant JSON::UnparserError

Things I tried:

  1. I tried removing my Gemfile.lock file and trying to do a fresh bundle install but that did not fix it.
  2. I tried to uninstall all gem versions of puma and jsonapi-serializer from my computer and reinstalling them, but that did not fix it.

Here's how I fixed:

First I ran the command below to force clean up unused gems in the bundler directory even if --path is set:

bundle clean --force

Next, I ran the command below to reinstall my gems for the project:

bundle install

OR

bundle update

OR

bundle update --force

That's all.

I hope this helps.

Trail answered 25/3, 2021 at 1:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.