Alons Oh Alons Oh - 1 year ago 217
Javascript Question

Node Keystonejs unexpected token export/import

I'm running this Keystonejs node webapp and I'm trying to include a class I created for my ReactNative project, but I'm getting the 'export' unexpected token error and I can't find how to fix that :\, I updated node to 6.2.0, I simply run it like

node keystone
I've tried the
node --harmony keystone
parameter but it still fails, I've tried installing the
babel-core babel-cli babel-preset-es2015
and nothing, I still can create a function with the
(arg) => { /*body*/}
form, although I don't even know in what version of ECMASCRIPT was that introduced xD. My class is declared just like that:
export default class API { ... }
API is my class name, and I intend to use it just like

import API from "API";

Actually I get the unexpected token 'import' before I can get the export, I changed it to a 'require' instead, and then I get the export error (sorry I'm trying anything xD)
I'm working on mac, I built the keystone project with the
yo keystone
and I've set up my
just fine, the thing works well until I try to add my class, any help? plz :\

I also used the
keywords in my class methods and I think that may cause trouble too XD, specially because I saw the 'async' package on the package.json file :(, any help would be greatly appreciated, thank you :3

Answer Source

The error occurs because NodeJS has not implemented modules in version 6.20. You can obtain a list of features that are being worked on by running the command:

node --v8-options | grep "in progress"

You should see this line:

--harmony_modules (enable "harmony modules" (in progress))

To utilize modules (import, export), you'll need to use babel to compile your Javascript files in ES2015 to ES5. You can create a simple script to do this in your package.json:

"compile": "./node_modules/.bin/babel -d dist/ src/"

The script assumes you have babel installed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download