-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uncaught TypeError: Class constructor [extended class] cannot be invoked without 'new' #839
Comments
I had a similar issue. I was extending a class exported from a node module. Upon inspecting the compiled code, I noticed that my class was transpiled into an ES5 class, but the code coming from the node modules was not being transpiled (it was still defined with the So it was essentially an ES5 class extending an ES6 class, and that seems to cause this error. I haven't managed to fix it yet, but it seems like one reason might be that Parcel isn't compiling the module. Here's the module in question.. Maybe it has something in common with |
@mthadley about |
After further investigation, it seems that babel won't compile node_modules, in webpack there are ways to mitigate this trough webpack. |
I have encountered the same problem using webpack+typescript. I use |
@mthadley describes this bug succinctly:
Parcel has compiled some of my I'm getting this error in version 1.8.1 |
I have a simple repro: test.html:<html>
<head>
<script>
//some other library
class Animal {
move(distance) {
console.log(`Animal moved ${distance}`);
}
}
</script>
</head>
<body>
<script src="test.js"></script>
</body>
</html> test.js:class Horse extends Animal {
move(distance) {
console.log(`Horse galloped ${distance}`);
}
}
new Animal().move(7);
new Horse().move(7); Output before Parcel:
Output after Parcel:
|
Here is the compiled var Horse = function (_Animal) {
_inherits(Horse, _Animal);
function Horse() {
_classCallCheck(this, Horse);
return _possibleConstructorReturn(this, (Horse.__proto__ || Object.getPrototypeOf(Horse)).apply(this, arguments));
}
_createClass(Horse, [{
key: "move",
value: function move(distance) {
console.log("Horse galloped " + distance);
}
}]);
return Horse;
}(Animal); |
Any news on a fix? I'm using [edit] Example here https://codesandbox.io/s/0pl314l77l |
bump... |
bump |
If you manage the dependent module's |
I got a fix to get it work with Parcel. This makes it to not remove the Add this to your
|
Adding It's probably due to parcel compiling using babel to correspond to specified browserlist as @alber70g pointed out already |
I think this real issue is due to the fact that parcel uses The correct behavior should be, when an es6 module is encountered, and the compile target is es5 and not es6, parcel should compile these into es5, despite them being node_modules. But that rises new questions: who should do this? Can parcel set these babel configs automatically, if so how does parcel know if your compile target is es6, or es5? Should that be identified from babel config (can they even be reliably)? This also walks you down the rabbit hole of should modules be compiled with the same config as your project? Now, that could have unforeseen side-effects. (Relevant: #13) If you're not bound by legacy browsers, I'd just set babel to ignore es6 class transformations and call it a day. The other issue is extending ES6 native classes like Long story short - ES6. |
@prasannavl the best practice is to transpile es6 as there are so many different es6 stages: So even module should be es5: |
@jantimon - Err, almost all modern platforms support a most of the commonly used features today, but not import and export. So, most modern libraries, take the polymer's And since we're specifically talking about es6 classes here, it's supported everywhere today unless you're concerned with legacy. The point being, there are and probably will be libraries that forgo es5, and just apply module to use es6. The only way to do it correctly moving forward would be to handle this case. PS: The |
@prasannavl what should the babelrc target be for ES6? @alber70g's latest 1 chrome version worked for me, but i want to make sure i have maximum browser compatibility. right now i'm just using @babel/preset-env. |
@timdeng2324 - perhaps, try adding |
Is there a fix that doesn't involve using browserslist? |
I had this problem too and after spending a day messing around with babel, I fixed it by changing the way I imported these (and don't use babel at all):
to:
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions. |
This will get fixed in Parcel 2 |
@DeMoorJasper last alpha release was in November of last year. Do you have an idea of when Parcel will be stable? |
@Mouvedia we currently do not have a timeline on when a stable Parcel 2 version will be out, we are however working on a list of features/bugfixes that are required to get it to stable so we can focus on getting it out asap without focusing too much on improvements and features we can add-on afterwards. I think it should be fixed in the current alpha, not entirely sure. |
To anyone arriving here having this problem with TypeScript and Parcel, you might need to add the following to a
And be sure to run parcel again with |
Choose one: is this a 🐛 bug report or 🙋 feature request?
🎛 Configuration (.babelrc, package.json, cli command)
🤔 Expected Behavior
It should just create the class and do the magic of Element. (wrench-set was created by me, and tested in another project, which works perfectly, using parcel 1.5.1)
😯 Current Behavior
it shoots error with class constructor cannot be invoked without new for the super()
Uncaught TypeError: Class constructor Element cannot be invoked without 'new'
at new Viewport (viewport.js:5)
at Object.require.4../index.scss (index.js:4)
at newRequire (3127ebea6fd59e02267b4119b100f294.js:42)
at require.14 (3127ebea6fd59e02267b4119b100f294.js:69)
💁 Possible Solution
the hacky way 😄
revert to 1.5.1 in order to be able to successfully compile the code
🔦 Context
💻 Code Sample
🌍 Your Environment
The text was updated successfully, but these errors were encountered: