SyntaxError: Unexpected token import on v6

1.9k Views Asked by At

Iam receiving SyntaxError: Unexpected token import even if my node is v6. What am I missing ? How do I make import Construct from 'can/construct/'; work for me instead of require ?

import Construct from 'can/construct/';  // Doesnt work
var Construct = require('can-construct'); // Works for me


var Animal = Construct.extend({
    sayHi: function(){
        console.log("hi")
    }
});

var animal = new Animal()
animal.sayHi();

I understand I need to be in ES6 to use import statement. But I already have it .

/usr/local/bin/node --version
v6.10.1

not sure package.json matters :

{
  "name": "canjstest",
  "version": "1.0.0",
  "description": "",
  "main": "canjs.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "Test",
  "dependencies": {
    "canjs": "^2.2.0"
  }
}

What am I missing ?

1

There are 1 best solutions below

0
Dominik On

ES6 modules are not supported in Node at all. There are some great article (one, two) around why it's been hard to decide how to implement it.

I suggest to transpile your code via babel.

A quick npm script would be:

"build:js": "babel src --out-dir dist --presets=es2015 --plugins=transform-runtime,transform-es2015-modules-commonjs",

While you need the following npm modules:

npm i babel-cli babel-plugin-transform-runtime babel-preset-es2015 --save-dev

Hope that helps