Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Webpack 5 Isn't Loading StimulusJS Controllers Via require.context()

I am migrating a Rails project from Webpacker to Webpack 5. The last hiccup is that Webpack isn't properly importing my Stimulus controllers. Per the Stimulus docs, I have this code in my entrypoint:

// app/javascript/global.js

import { Application } from "@hotwired/stimulus"
import { definitionsFromContext } from "@hotwired/stimulus-webpack-helpers"

window.Stimulus = Application.start()
const context = require.context("./src/controllers", true, /\.js$/)
Stimulus.load(definitionsFromContext(context))

The controllers themselves live in app/javascript/src/controllers.

The assets compile as expected, but the Stimulus controllers aren't registered. Moreover, when I log the output from require.context() above, I get the following console error:

function t(e){var t=new Error("Cannot find module '"+e+"'");throw t.code="MODULE_NOT_FOUND",t}

I know the file path to my controllers is correct, because when I change it to something obviously incorrect, the build fails. In addition, when I manually register a controller:

import { Application } from "@hotwired/stimulus"
import Tabs from "./tabs_controller";
const application = Application.start()
application.register("tabs", Tabs);

The controller loads and functions as expected, so I don't believe it's a Stimulus issue, specifically.

Am I configuring the require.context() method incorrectly? It's all boilerplate straight from the documentation, so it should work, but I'm at a loss for what else to troubleshoot.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!