Babel error: Foo class constructor cannot be called without 'new'

I use babel for forwarding.

I have a class BaseComponent that extends to class Logger .

When I run new Logger() in a browser, I get this error

The BaseComponent class constructor cannot be called without the "new"

code that calls this:

 var Logger = function (_BaseComponent) { _inherits(Logger, _BaseComponent); function Logger() { _classCallCheck(this, Logger); return _possibleConstructorReturn(this, Object.getPrototypeOf(Logger).call(this, "n")); //throws here } 
+6
source share
2 answers

Due to the way ES6 classes work, you cannot extend your own class with the transpiled class. If your platform supports native classes, my recommendation would be, instead of using the es2015 preset, use es2015-node5 if you are on Node 5. This will force Babel to skip class compilation so that your code uses native classes and the native classes can distribute other native classes.

+9
source

Another solution is to include { exclude: ["transform-es2015-classes"] } in .babelrc

 presets: [ ["env", { exclude: ["transform-es2015-classes"] }] ] 

UPDATE: In the latest version of "env", the names of the predefined plugins have changed (for example, now it is "transform-classes"). Use the debug option to check which plugins are enabled.

+6
source

Source: https://habr.com/ru/post/1233102/


All Articles