0
votes

I'm getting an error saying "unexpected token export" when trying to compile some es6 code using "babel-cli" with the presets "es2015" and "stage-2". I'm trying to export a function from the "test.js" file and import it in the "index.js" file but for some reason it doesn't accept "export" in "test.js".

Each module is installed locally, so I run it from the package.json "scripts" using "build: babel server/index.js -o server/index.babel.js".

My ".babelrc" file consists of:

 {
  "presets": ["es2015", "stage-2"]
 }

test.js:

const test = (msg) => {
    console.log(msg)
};

export default test;

index.js:

index.js:

import test from './test'

test("Hello")

Any help is much appreciated.

1
did you babelify test.js ?Ja9ad335h
What do you mean by babelify?2K01B5
babelify is converting es6 to es5 using babel. i see you doing it for index.js in "scripts build:". you also need to do the same for test.jsJa9ad335h

1 Answers

0
votes

Your code looks fine, and the fact that you're getting that error means that it is reading the files properly. Make sure that you downloaded stage-2 correctly, but I think your error is in that export default is still in stage-1.

Run npm install --save-dev babel-preset-stage-1 and add stage-1 to your presets.

That should do it. Here's a link to https://github.com/leebyron/ecmascript-export-default-from and https://github.com/leebyron/ecmascript-export-ns-from