question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Class constructor Controller cannot be invoked without 'new' (3.0.0-beta1)

See original GitHub issue

Just had a go at updating our Stimulus 2.x app to the beta. We’re using Rails/Webpacker.

I think there’s some missing parts of the docs around this change (in the push to importmaps) so I did have to guess some of the new Webpack configuration code. Namely - definitionsFromContext used to come from stimulus/webpack-helpers and I think it now comes from just @hotwired/stimulus.

import { Application } from "@hotwired/stimulus";
import { definitionsFromContext } from "@hotwired/stimulus";

const application = Application.start();
const context = require.context("./controllers", true, /\.js$/);
application.load(definitionsFromContext(context));

This runs, however in the browser I get the following error - following the stacktrace it occurs in each of my controller modules.

Uncaught (in promise) TypeError: Class constructor Controller cannot be invoked without 'new'
    at new _default (application.js:3048)
    at new extended (vendors-node_modules_babel_runtime_regenerator_index_js-node_modules_hotwired_stimulus_dist_s-68817b.js:1338)
    at new Context (vendors-node_modules_babel_runtime_regenerator_index_js-node_modules_hotwired_stimulus_dist_s-68817b.js:1198)
    at Module.fetchContextForScope (vendors-node_modules_babel_runtime_regenerator_index_js-node_modules_hotwired_stimulus_dist_s-68817b.js:1400)
    at Module.connectContextForScope (vendors-node_modules_babel_runtime_regenerator_index_js-node_modules_hotwired_stimulus_dist_s-68817b.js:1386)
    at Router.scopeConnected (vendors-node_modules_babel_runtime_regenerator_index_js-node_modules_hotwired_stimulus_dist_s-68817b.js:1705)
    at ScopeObserver.elementMatchedValue (vendors-node_modules_babel_runtime_regenerator_index_js-node_modules_hotwired_stimulus_dist_s-68817b.js:1623)
    at ValueListObserver.tokenMatched (vendors-node_modules_babel_runtime_regenerator_index_js-node_modules_hotwired_stimulus_dist_s-68817b.js:814)
    at TokenListObserver.tokenMatched (vendors-node_modules_babel_runtime_regenerator_index_js-node_modules_hotwired_stimulus_dist_s-68817b.js:747)
    at vendors-node_modules_babel_runtime_regenerator_index_js-node_modules_hotwired_stimulus_dist_s-68817b.js:741

Nothing fancy going on with my controllers - just updated the import from stimulus to @hotwired/stimulus as expected.

import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
  //
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

33reactions
m3xqcommented, Nov 1, 2021

In package.json add "not IE 11" to "browserslist"

"browserslist": [
    "defaults",
    "not IE 11"
  ]
5reactions
dwightwatsoncommented, Nov 1, 2021

@m3xq’s suggestion appears to have done the trick for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Class constructor Controller cannot be invoked without 'new ...
I migrated from "stimulus": "^2.0.0" to "@hotwired/stimulus": "^3.0.0" I got an error Uncaught TypeError: Class constructor Controller ...
Read more >
Upgraded to Stimulus 3, console error Cannot call a class ...
Now, all my controllers generates the console error Unhandled Promise Rejection: TypeError: Cannot call a class constructor without |new| .
Read more >
Class constructor Class cannot be invoked without 'new'
I'm getting the following error in the console of the browser, when trying to access my route “login”: Uncaught (in promise) TypeError: ...
Read more >
Javascript ES6 TypeError Class constructor Client cannot be ...
When I try to execute nodemon command I always see this error TypeError: Class constructor Client cannot be invoked without 'new'.
Read more >
stimulusjs - Bountysource
Having two NOT NESTED elements using the same controller, like here: ... Class constructor Controller cannot be invoked without 'new' (3.0.0-beta1) $ 0....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found