Home > Blockchain >  Rails 6, Heroku: NameError: uninitialized constant Rack::Cors
Rails 6, Heroku: NameError: uninitialized constant Rack::Cors

Time:07-08

I'm trying to deploy my API to Heroku but it keeps giving me this error NameError: uninitialized constant Rack::Cors

Here is my cors.rb file

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'

    resource '*',
             headers: :any,
             methods: %i[get post put patch delete options head]
  end
end

and here is Hroku build log

-----> Building on the Heroku-20 stack
-----> Using buildpacks:
       1. heroku/nodejs
       2. heroku/ruby
-----> Node.js app detected
       
-----> Creating runtime environment
       
       NPM_CONFIG_LOGLEVEL=error
       USE_YARN_CACHE=true
       NODE_VERBOSE=false
       NODE_ENV=production
       NODE_MODULES_CACHE=true
       
-----> Installing binaries
       engines.node (package.json):  unspecified
       engines.npm (package.json):   unspecified (use default)
       engines.yarn (package.json):  unspecified (use default)
       
       Resolving node version 16.x...
       Downloading and installing node 16.15.1...
       Using default npm version: 8.11.0
       Resolving yarn version 1.22.x...
       Downloading and installing yarn (1.22.19)
       Installed yarn 1.22.19
       
-----> Installing dependencies
       Installing node modules (yarn.lock)
       yarn install v1.22.19
       [1/4] Resolving packages...
       [2/4] Fetching packages...
       [3/4] Linking dependencies...
       [4/4] Building fresh packages...
       Done in 14.52s.
       
-----> Build
       
-----> Pruning devDependencies
       yarn install v1.22.19
       [1/4] Resolving packages...
       [2/4] Fetching packages...
       [3/4] Linking dependencies...
       [4/4] Building fresh packages...
       warning Ignored scripts due to flag.
       Done in 2.82s.
       
-----> Caching build
       - yarn cache
       
-----> Build succeeded!
-----> Ruby app detected
-----> Installing bundler 2.3.10
-----> Removing BUNDLED WITH version in the Gemfile.lock
-----> Compiling Ruby/Rails
-----> Using Ruby version: ruby-2.7.5
-----> Installing dependencies using bundler 2.3.10
       Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
       Your Gemfile lists the gem devise (>= 0) more than once.
       You should probably keep only one of them.
       Remove any duplicate entries and specify the gem only once.
       While it's not a problem now, it could cause errors if you change the version of one of them later.
       Your Gemfile lists the gem devise-jwt (>= 0) more than once.
       You should probably keep only one of them.
       Remove any duplicate entries and specify the gem only once.
       While it's not a problem now, it could cause errors if you change the version of one of them later.
       Your Gemfile lists the gem rack-cors (>= 0) more than once.
       You should probably keep only one of them.
       Remove any duplicate entries and specify the gem only once.
       While it's not a problem now, it could cause errors if you change the version of one of them later.
       Fetching gem metadata from https://rubygems.org/.........
       Fetching rake 13.0.6
       Installing rake 13.0.6
       Fetching concurrent-ruby 1.1.9
       Fetching minitest 5.15.0
       Fetching builder 3.2.4
       Fetching zeitwerk 2.5.4
       Installing zeitwerk 2.5.4
       Installing concurrent-ruby 1.1.9
       Installing minitest 5.15.0
       Installing builder 3.2.4
       Fetching erubi 1.10.0
       Fetching mini_portile2 2.7.1
       Installing erubi 1.10.0
       Fetching racc 1.6.0
       Installing mini_portile2 2.7.1
       Fetching crass 1.0.6
       Installing racc 1.6.0 with native extensions
       Installing crass 1.0.6
       Fetching rack 2.2.3
       Fetching nio4r 2.5.8
       Installing nio4r 2.5.8 with native extensions
       Installing rack 2.2.3
       Fetching marcel 1.0.2
       Fetching websocket-extensions 0.1.5
       Installing websocket-extensions 0.1.5
       Installing marcel 1.0.2
       Fetching mini_mime 1.1.2
       Fetching msgpack 1.4.4
       Installing mini_mime 1.1.2
       Installing msgpack 1.4.4 with native extensions
       Using bundler 2.3.10
       Fetching ffi 1.15.5
       Installing ffi 1.15.5 with native extensions
       Fetching method_source 1.0.0
       Installing method_source 1.0.0
       Fetching pg 1.3.1
       Installing pg 1.3.1 with native extensions
       Fetching thor 1.2.1
       Installing thor 1.2.1
       Fetching tilt 2.0.10
       Installing tilt 2.0.10
       Fetching semantic_range 3.0.0
       Installing semantic_range 3.0.0
       Fetching turbolinks-source 5.2.0
       Installing turbolinks-source 5.2.0
       Fetching i18n 1.9.1
       Installing i18n 1.9.1
       Fetching tzinfo 2.0.4
       Installing tzinfo 2.0.4
       Fetching rack-test 1.1.0
       Installing rack-test 1.1.0
       Fetching rack-proxy 0.7.2
       Installing rack-proxy 0.7.2
       Fetching sprockets 4.0.2
       Installing sprockets 4.0.2
       Fetching websocket-driver 0.7.5
       Installing websocket-driver 0.7.5 with native extensions
       Fetching mail 2.7.1
       Installing mail 2.7.1
       Fetching nokogiri 1.13.1 (x86_64-linux)
       Installing nokogiri 1.13.1 (x86_64-linux)
       Fetching puma 5.6.1
       Installing puma 5.6.1 with native extensions
       Fetching turbolinks 5.2.1
       Installing turbolinks 5.2.1
       Fetching activesupport 6.1.4.4
       Installing activesupport 6.1.4.4
       Fetching bootsnap 1.10.3
       Installing bootsnap 1.10.3 with native extensions
       Fetching loofah 2.13.0
       Installing loofah 2.13.0
       Fetching sassc 2.4.0
       Installing sassc 2.4.0 with native extensions
       Fetching rails-dom-testing 2.0.3
       Installing rails-dom-testing 2.0.3
       Fetching globalid 1.0.0
       Installing globalid 1.0.0
       Fetching activemodel 6.1.4.4
       Installing activemodel 6.1.4.4
       Fetching rails-html-sanitizer 1.4.2
       Installing rails-html-sanitizer 1.4.2
       Fetching activejob 6.1.4.4
       Installing activejob 6.1.4.4
       Fetching activerecord 6.1.4.4
       Installing activerecord 6.1.4.4
       Fetching actionview 6.1.4.4
       Installing actionview 6.1.4.4
       Fetching actionpack 6.1.4.4
       Installing actionpack 6.1.4.4
       Fetching jbuilder 2.11.5
       Installing jbuilder 2.11.5
       Fetching actioncable 6.1.4.4
       Installing actioncable 6.1.4.4
       Fetching activestorage 6.1.4.4
       Installing activestorage 6.1.4.4
       Fetching actionmailer 6.1.4.4
       Installing actionmailer 6.1.4.4
       Fetching railties 6.1.4.4
       Installing railties 6.1.4.4
       Fetching sprockets-rails 3.4.2
       Installing sprockets-rails 3.4.2
       Fetching actionmailbox 6.1.4.4
       Installing actionmailbox 6.1.4.4
       Fetching actiontext 6.1.4.4
       Installing actiontext 6.1.4.4
       Fetching webpacker 5.4.3
       Installing webpacker 5.4.3
       Fetching rails 6.1.4.4
       Installing rails 6.1.4.4
       Fetching sassc-rails 2.1.2
       Installing sassc-rails 2.1.2
       Fetching sass-rails 6.0.0
       Installing sass-rails 6.0.0
       Bundle complete! 28 Gemfile dependencies, 57 gems now installed.
       Gems in the groups 'development' and 'test' were not installed.
       Bundled gems are installed into `./vendor/bundle`
       Bundle completed (182.45s)
       Cleaning up the bundler cache.
-----> Detecting rake tasks
-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       rake aborted!
       NameError: uninitialized constant Rack::Cors
       /tmp/build_6c572622/config/initializers/cors.rb:1:in `<main>'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.10.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:48:in `load'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.10.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:48:in `load'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/railties-6.1.4.4/lib/rails/engine.rb:681:in `block in load_config_initializer'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4.4/lib/active_support/notifications.rb:205:in `instrument'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/railties-6.1.4.4/lib/rails/engine.rb:680:in `load_config_initializer'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/railties-6.1.4.4/lib/rails/engine.rb:634:in `block (2 levels) in <class:Engine>'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/railties-6.1.4.4/lib/rails/engine.rb:633:in `each'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/railties-6.1.4.4/lib/rails/engine.rb:633:in `block in <class:Engine>'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/railties-6.1.4.4/lib/rails/initializable.rb:32:in `instance_exec'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/railties-6.1.4.4/lib/rails/initializable.rb:32:in `run'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/railties-6.1.4.4/lib/rails/initializable.rb:61:in `block in run_initializers'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/railties-6.1.4.4/lib/rails/initializable.rb:50:in `each'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/railties-6.1.4.4/lib/rails/initializable.rb:50:in `tsort_each_child'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/railties-6.1.4.4/lib/rails/initializable.rb:60:in `run_initializers'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/railties-6.1.4.4/lib/rails/application.rb:391:in `initialize!'
       /tmp/build_6c572622/config/environment.rb:5:in `<main>'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.10.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.10.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.5.4/lib/zeitwerk/kernel.rb:35:in `require'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/railties-6.1.4.4/lib/rails/application.rb:367:in `require_environment!'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/railties-6.1.4.4/lib/rails/application.rb:533:in `block in run_tasks_blocks'
       /tmp/build_6c572622/vendor/bundle/ruby/2.7.0/gems/sprockets-rails-3.4.2/lib/sprockets/rails/task.rb:61:in `block (2 levels) in define'
       Tasks: TOP => environment
       (See full trace by running task with --trace)
 !
 !     Precompiling assets failed.
 !
 !     Push rejected, failed to compile Ruby app.
 !     Push failed

Anyone could help please? Thanks in advance

CodePudding user response:

Okay, I solved the issue guys, in the build logs you can see the following: Gems in the groups 'development' and 'test' were not installed. so I created a group for production gems

group :production do
  # Add the gems you need on production here
end

and when I re-deployed, it worked fine.

  • Related