How It Works Features Pricing Blog Error Guides
Log In Start Free Trial
Rails · Ruby

Fix Importmap::Engine::Error: Could not find javascript module 'controllers/hello_controller' in importmap in Rails

This error occurs when importmap-rails cannot resolve a JavaScript module referenced in your code. The module is not pinned in config/importmap.rb or the file path does not match the pin name. Add the missing pin to your importmap configuration or verify the file exists at the expected path under app/javascript.

Reading the Stack Trace

Importmap::Engine::Error (Could not find javascript module 'controllers/hello_controller' in importmap): importmap-rails (2.0.1) lib/importmap/map.rb:55:in `resolve' importmap-rails (2.0.1) lib/importmap/map.rb:32:in `to_json' app/views/layouts/application.html.erb:12:in `_app_views_layouts_application_html_erb__render' actionview (7.1.3) lib/action_view/renderer/template_renderer.rb:62:in `render_template' actionpack (7.1.3) lib/action_controller/metal/streaming.rb:219:in `_render_template'

Here's what each line means:

Common Causes

1. Module not pinned in importmap.rb

The JavaScript module file exists but is not registered in config/importmap.rb.

# config/importmap.rb
pin 'application', preload: true
# Missing: pin_all_from 'app/javascript/controllers', under: 'controllers'

2. File path does not match pin

The pin name does not match the file location on disk.

# config/importmap.rb
pin 'controllers/hello_controller'
# But file is at app/javascript/controllers/hello-controller.js (hyphen vs underscore)

3. Missing vendor module pin

A third-party library imported in JavaScript is not pinned from a CDN or vendor directory.

// app/javascript/application.js
import 'lodash'  // Not pinned in importmap.rb

The Fix

Use pin_all_from to automatically pin all JavaScript files in the controllers directory. This maps every file in app/javascript/controllers to a module name under the controllers namespace, so Stimulus controllers are automatically discoverable.

Before (broken)
# config/importmap.rb
pin 'application', preload: true
# No controller pins defined
After (fixed)
# config/importmap.rb
pin 'application', preload: true
pin '@hotwired/turbo-rails', to: 'turbo.min.js', preload: true
pin '@hotwired/stimulus', to: 'stimulus.min.js', preload: true
pin_all_from 'app/javascript/controllers', under: 'controllers'

Testing the Fix

require 'rails_helper'

RSpec.describe 'Importmap configuration', type: :request do
  it 'renders the layout without importmap errors' do
    get root_path
    expect(response).to have_http_status(:ok)
  end

  it 'includes importmap script tag' do
    get root_path
    expect(response.body).to include('importmap')
  end

  it 'resolves all pinned modules' do
    importmap = Rails.application.importmap
    expect { importmap.to_json(resolver: ActionController::Base.helpers) }.not_to raise_error
  end
end

Run your tests:

bundle exec rspec spec/requests/importmap_spec.rb

Pushing Through CI/CD

git checkout -b fix/rails-importmap-module,git add config/importmap.rb,git commit -m "fix: pin all stimulus controllers in importmap configuration",git push origin fix/rails-importmap-module

Your CI config should look something like this:

name: CI
on:
  pull_request:
    branches: [main]
jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_PASSWORD: postgres
        ports: ['5432:5432']
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.3'
          bundler-cache: true
      - run: bin/rails db:setup
      - run: bundle exec rspec

The Full Manual Process: 18 Steps

Here's every step you just went through to fix this one bug:

  1. Notice the error alert or see it in your monitoring tool
  2. Open the error dashboard and read the stack trace
  3. Identify the file and line number from the stack trace
  4. Open your IDE and navigate to the file
  5. Read the surrounding code to understand context
  6. Reproduce the error locally
  7. Identify the root cause
  8. Write the fix
  9. Run the test suite locally
  10. Fix any failing tests
  11. Write new tests covering the edge case
  12. Run the full test suite again
  13. Create a new git branch
  14. Commit and push your changes
  15. Open a pull request
  16. Wait for code review
  17. Merge and deploy to production
  18. Monitor production to confirm the error is resolved

Total time: 30-60 minutes. For one bug.

Or Let bugstack Fix It in Under 2 minutes

Every step above? bugstack does it automatically.

Step 1: Install the SDK

gem install bugstack

Step 2: Initialize

require 'bugstack'

Bugstack.init(api_key: ENV['BUGSTACK_API_KEY'])

Step 3: There is no step 3.

bugstack handles everything from here:

  1. Captures the stack trace and request context
  2. Pulls the relevant source files from your GitHub repo
  3. Analyzes the error and understands the code context
  4. Generates a minimal, verified fix
  5. Runs your existing test suite
  6. Pushes through your CI/CD pipeline
  7. Deploys to production (or opens a PR for review)

Time from error to fix deployed: Under 2 minutes.

Human involvement: zero.

Try bugstack Free →

No credit card. 5-minute setup. Cancel anytime.

Deploying the Fix (Manual Path)

  1. Update config/importmap.rb with the correct pins.
  2. Verify all JavaScript modules resolve with bin/importmap audit.
  3. Run the test suite.
  4. Open a pull request.
  5. Merge and verify JavaScript loads correctly in staging.

Frequently Asked Questions

BugStack runs the fix through your existing test suite, generates additional edge-case tests, and validates that no other components are affected before marking it safe to deploy.

BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.

Importmap is simpler and does not require a build step. Use jsbundling-rails with esbuild if you need npm packages, TypeScript, or JSX compilation.

Use bin/importmap pin package-name to pin it from jspm.io CDN, or download it to vendor/javascript and pin it locally.