Koa: TypeError: Class constructors cannot be invoked without 'new'

Created on 28 Oct 2015  Â·  13Comments  Â·  Source: koajs/koa

Trying to test koa 2 alpha, getting this error:

constructor() {
10/28/15 06:20:06
aidax-collector-1 aidax[1491]: ^
10/28/15 06:20:06
aidax-collector-1 aidax[1491]: TypeError: Class constructors cannot be invoked without 'new'
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at require (module.js:384:17)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at normalLoader (/app/releases/20151028201122/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:199:5)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at startup (node.js:117:18)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Function.Module.runMain (module.js:475:10)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Module._compile (module.js:434:26)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Object.Module._extensions..js (module.js:452:10)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Module.load (module.js:355:32)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Function.Module._load (module.js:310:12)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Object.<anonymous> (/app/releases/20151028201122/node_modules/koa-cluster/lib/http.js:9:11)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at require (module.js:384:17)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Module.require (module.js:365:17)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Function.Module._load (module.js:310:12)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Module.load (module.js:355:32)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Object.<anonymous> (/app/releases/20151028201122/index.js:6:1)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Module._compile (module.js:434:26)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Object.Module._extensions..js (module.js:452:10)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at node.js:951:3
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Module.require (module.js:365:17)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Module.load (module.js:355:32)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Function.Module._load (module.js:310:12)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Object.require.extensions.(anonymous function) [as .js] (/app/releases/20151028201122/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:216:7)
10/28/15 06:20:05
aidax-collector-1 aidax[1491]: at Module._compile (module.js:434:26)

Most helpful comment

In koa2 you need to do const app = new Koa() instead of const app = koa()

All 13 comments

In koa2 you need to do const app = new Koa() instead of const app = koa()

Thanks, that is easy to miss

Yeah, we need a major changes doc b/w v1 and v2 :P

That said, even though I submitted the PR for making Application a class, I'm still meh about it. There's little to no practical use of 2 "Koa"s in 1 app so not sure why it should be a class vs a prototype constructor function... but looks like that's where the community is going so ¯_(ツ)_/¯

Quick note on this, if you are using Koa 2 and trying to do something like this

class MyServer extends Koa {
  constructor() {
    super();
    this.use(convert(serve('./dist')));
  }
}

const app = new MyServer();

You will get this error if you are using the full monty babel presets, unless you choose not to ignore Koa in node_modules

{
  "presets": ["es2015", "stage-0", "react"]
}

Instead I only use the presets I need and let Node 5 support the rest and it works

const babelrcServer = `
  {
    "babelrc": false,
    "presets": ["react"],
    "plugins": [
      "transform-es2015-for-of",
      "syntax-object-rest-spread",
      "transform-es2015-parameters",
      "transform-object-rest-spread",
      "syntax-async-functions",
      "transform-es2015-arrow-functions",
      "transform-es2015-spread",
      "transform-es2015-destructuring",
      "transform-es2015-modules-commonjs",
      "transform-strict-mode",
      "transform-regenerator"
    ]
  }
`;

require('babel-register')(JSON.parse(babelrcServer));

I'm having trouble with the error you are talking about? Any ideas?

@julie1013 Code? Did you see https://github.com/koajs/koa/issues/568#issuecomment-151983902?

For a class I'm in... for example, I had a FarmAnimal class and I did this:

class Cow extends FarmAnimal(){
constructor(name, sound, image){
super("Milky", "moo", "images/cow.jpg");
}
}

But I got "Class FarmAnimal cannot be invoked without 'new'"

@julie1013 Is this even related to Koa? I'm assuming you were just replacing keywords there, but try stackoverflow.

It looks like this is third in the list of a "class constructors cannot be invoked without 'new'" Google search, we should consider locking this thread with a quick explanation.

Sorry, I guess I am in the wrong place and was just desperately looking for an answer! :)

@julie1013 if you are using Babel depending upon your presets/plugins you can see this error. Make sure not to compile your classes. Have a look at this babelrc and maybe it works https://github.com/dtothefp/build-boiler/blob/master/.babelrc

@julie1013 You've got a syntax error.

class Cow extends FarmAnimal(){
                            ^^

The parentheses shouldn't be there.

EDIT: Just noticed this was over a year ago.

@julie1013 You've got a syntax error.

class Cow extends FarmAnimal(){
                            ^^

The parentheses shouldn't be there.

EDIT: Just noticed this was over a year ago.

Thanks, that helped!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

felixfbecker picture felixfbecker  Â·  5Comments

TheRav3n picture TheRav3n  Â·  3Comments

rally25rs picture rally25rs  Â·  4Comments

ilkkao picture ilkkao  Â·  4Comments

ElegantScripting picture ElegantScripting  Â·  5Comments