marvel308
marvel308

Reputation: 10458

Node.js - SyntaxError: Unexpected token import while running on node 6.10.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

Upvotes: 2

Views: 4287

Answers (2)

Fazal Rasel
Fazal Rasel

Reputation: 4526

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

Upvotes: 3

mJehanno
mJehanno

Reputation: 866

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

Upvotes: -1

Related Questions