0

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.

Michael Jungo
  • 31,583
  • 3
  • 91
  • 84
2K01B5
  • 1,011
  • 1
  • 10
  • 23

1 Answers1

0

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

Brett East
  • 4,022
  • 2
  • 20
  • 31