Javascript ES6 TypeError: Class constructor Client cannot be invoked without 'new'

后端 未结 6 710
深忆病人
深忆病人 2020-11-30 02:59

I have a class written in Javascript ES6. When I try to execute nodemon command I always see this error TypeError: Class constructor Client cannot be invo

相关标签:
6条回答
  • 2020-11-30 03:27

    I hope you have already been able to solve your problem, here is my solution for this error:

    Blockquote

    class indexRoutes
    {
    
        public  router: Router= Router();
    }
    const INDEX_ROUTES = new indexRoutes();
    export default INDEX_ROUTES.router;
    
    0 讨论(0)
  • 2020-11-30 03:36

    In package.json you can use targets configuration with @babel/preset-env. set the esmodules as 'true'.

    Below is the example how I am using in my file:

      "babel": {
        "presets": [
          [
            "@babel/preset-env",
            {
              "targets": {
                "esmodules": true
              }
            }
          ],
          "@babel/preset-react",
          "@babel/preset-flow"
        ],
        "plugins": [
          "@babel/plugin-proposal-class-properties"
        ]
      },
    
    0 讨论(0)
  • 2020-11-30 03:41

    The problem is that the class extends native ES6 class and is transpiled to ES5 with Babel. Transpiled classes cannot extend native classes, at least without additional measures.

    class TranspiledFoo extends NativeBar {
      constructor() {
        super();
      }
    }
    

    results in something like

    function TranspiledFoo() {
      var _this = NativeBar.call(this) || this;
      return _this;
    }
    // prototypically inherit from NativeBar 
    

    Since ES6 classes should be only called with new, NativeBar.call results in error.

    ES6 classes are supported in any recent Node version, they shouldn't be transpiled. es2015 should be excluded from Babel configuration, it's preferable to use env preset set to node target.

    The same problem applies to TypeScript. The compiler should be properly configured to not transpile classes in order for them to inherit from native or Babel classes.

    0 讨论(0)
  • 2020-11-30 03:46

    I was transpiling not Javascript but Typescript, and ran into the same problem.

    I edited the Typescript compiler config file, tsconfig.json, to generate ES2017 Javascript code:

    {
        "compilerOptions": {
            "target": "ES2017",
    

    instead of whatever the default is, ES2015? — then all worked fine.

    (Maybe this answer can be helpful for people who use Typescript and find this question when they search for the same error message, like I did.)

    0 讨论(0)
  • 2020-11-30 03:49

    If you are not using babel and simple typescript. I got the error in material. Try to reduce the versions and bring them down to similar versions. My packages were having variety of versions.I solved the issue by lowering the package versions of packages like cdk, material.

    Check the image of my final package.json

    0 讨论(0)
  • 2020-11-30 03:54

    For those who are using ts-node, it could be possible that your tsconfig.json is unable to be loaded by ts-node.
    First make sure you've set the below option for tsconfig.json:

    {
        "compilerOptions": {
            "target": "ES6",
            ...
        }
    }
    

    Then you may try ts-node --script-mode or use --project to specify the path to your tsconfig.json.

    0 讨论(0)
提交回复
热议问题