2

I am running node version 6.10.2 I am trying to run this peice of code

import * as events from "events"
class MyClass extends events.EventEmitter {
    constructor(x, y){
        this.x = x;
        this.y = y;
    }
    compute(){
        return this.x * this.y;
    }
}
var vow = new MyClass(2,3);
vow.compute();

I am getting this error, How can I make it run?

SyntaxError: Unexpected token import

marvel308
  • 10,288
  • 1
  • 21
  • 32
  • Possible duplicate of [ES2015 "import" not working in node v6.0.0 with with --harmony\_modules option](http://stackoverflow.com/questions/36901147/es2015-import-not-working-in-node-v6-0-0-with-with-harmony-modules-option) – Fazal Rasel Apr 21 '17 at 14:27

2 Answers2

3

Nodejs version 6.. do not support import and export. It cover 96% of es6.

So, you have to use babel to covert your es6 code to es5 version if you like to use Nodejs 6.

take a look at

Fazal Rasel
  • 4,446
  • 2
  • 20
  • 31
-1

I'm not sure about it but i think node 6.x doesn't handle import so you either need a transpiler like babel which will convert your es6 code into es5, you can use require instead of import or upgrade to node 7.x which should support import style. (You can use nvm to have many versions of node on your computer).

mJehanno
  • 836
  • 1
  • 12
  • 19