goatslacker / alt

Isomorphic flux implementation
http://alt.js.org/
3.45k stars 321 forks source link

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

Open jkcom opened 9 years ago

jkcom commented 9 years ago

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?

goatslacker commented 9 years ago

Let me look into it

goatslacker commented 9 years ago

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.

jkcom commented 9 years ago

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.

goatslacker commented 9 years ago

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

jkcom commented 9 years ago

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

srph commented 9 years ago

reopen?

goatslacker commented 9 years ago

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

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

krrg commented 8 years ago

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

Jdavid1001-zz commented 8 years ago

@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()

chyzwar commented 8 years ago

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.

OmriBH commented 8 years ago

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).

sjatkins commented 8 years ago

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.