alt icon indicating copy to clipboard operation
alt copied to clipboard

io.js, ES6: TypeError: Class constructors cannot be invoked without 'new'

Open jkcom opened this issue 9 years ago • 12 comments

When trying to run server-side using io.js (2.3.3) using es6 classes i get the following error:

/Users/josef/Develop/some/some-app/src/js/shared/stores/some-store.js:43
  constructor() {
             ^
TypeError: Class constructors cannot be invoked without 'new'
    at Store.SomeStore (/Users/josef/Develop/some/some-app/src/js/shared/stores/some-store.js:43:14)
    at new Store (/Users/josef/Develop/some/some-app/node_modules/alt/lib/store/index.js:139:73)
    at Object.createStoreFromClass (/Users/josef/Develop/some/some-app/node_modules/alt/lib/store/index.js:160:15)
    at Alt.createStore (/Users/josef/Develop/some/some-app/node_modules/alt/lib/index.js:116:109)
   ...

From a store in this format:

'use strict';
var alt = require('../alt'),
    someActions = require('../actions/some-actions'):

class SomeStore {
  constructor() {

    this.state = {};

    this.bindListeners({
      onSomeCreated: someActions.someCreated
    });
  }

  onSomeCreated(some) {
  }
}

module.exports = alt.createStore(SomeStore, 'SomeStore');

Is there some configuration that should be set for alt to know how initialisation should happen? Or could I be missing some other obvious thing?

jkcom avatar Jul 09 '15 09:07 jkcom

Let me look into it

goatslacker avatar Jul 09 '15 17:07 goatslacker

This looks like a babel issue off the bat. I don't mind publishing an ES6 strict version of alt but I don't think that's the right solutions.

goatslacker avatar Jul 10 '15 03:07 goatslacker

Thank you for your quick reply.

My goal is to run the iojs server without any cross compiler (https://iojs.org/en/es6.html). So from what I understand when it fails on the server it is not run through babel at all. But it very likely have something to do with the strict-mode of the class.

Maybe a setup like this is a bit premature.. but as far as i can tell the specification states use of class require strict mode.. or am i missing something?

Thanks, and by the way.. this framework is an absolute pleasure to work with.

jkcom avatar Jul 10 '15 06:07 jkcom

Alt is transpiled by babel into es5 code. You're most likely running that on the server.

goatslacker avatar Jul 10 '15 17:07 goatslacker

True, that was the issue.. I was able to solve it by wrapping my server application in a bable module loader/transpiler to transpile to es5 during runtime. Now I only need to fix the same problem with the jest test-runner but that is another thing.. Thanks for helping out. Best, Josef

jkcom avatar Jul 13 '15 08:07 jkcom

reopen?

srph avatar Jul 14 '15 10:07 srph

Broken behavior: https://gist.github.com/goatslacker/c0c1bccc3ea5224f5bd8

Apparently you can't derive a "native" class in babel.

goatslacker avatar Jul 14 '15 19:07 goatslacker

Is there any update on this? We're also running into this same issue...

krrg avatar Feb 04 '16 19:02 krrg

@krrg , we were having the same issue. It seems to arise when you do not reactify with es6. You have to let reactify know you are using es6.

For example, it works in his tutorial because he passed it with the flag --es6 when he goes to build: browserify -t [reactify --es6] src/App.jsx > build/app.js

It works for us in our gulp file since we pass it like so: browserify(p.jsx) .transform('reactify', {stripTypes: true, es6: true}) .bundle()

Jdavid1001-zz avatar Feb 22 '16 18:02 Jdavid1001-zz

I have similar issues.

TypeError: Class constructors cannot be invoked without 'new'
    at ActionsGenerator.FeedActions (/home/raziel/MyProjects/legion-media/isomorphic/build/server.js:463:2)

I think it is related to #604

I will take a look tomorrow.

chyzwar avatar Mar 13 '16 22:03 chyzwar

Solution - import Alt from 'alt/src/index', instead from 'Alt' itself. If your Babel is configured not to translate classes to functions, then both your code and Alt implementation would be in class-like structure, and it should work fine.

In order for Babel to read the sources of Alt, you should add syntax-trailing-function-commas to the Babel plugins.

I tested this on the latest chrome, and it worked fine (before, it indeed failed with the 'new' error).

OmriBH avatar Apr 29 '16 08:04 OmriBH

A full spec of what it took, say the packages.json and .babelrc would be nice. It is very frustrating banging on these until it works. Especially when also translating from browserify or others to webpack.

sjatkins avatar Jun 27 '16 21:06 sjatkins