Is it possible to export Arrow functions in ES6/7?

jozzy picture jozzy · Apr 6, 2016 · Viewed 56.6k times · Source

The export statement below gives a syntax error

export default const hello = () => console.log("say hello")

why ?

I'm only able to export named functions

export function hello() {
  console.log("hello")
}

What is the reason?

Answer

Felix Kling picture Felix Kling · Apr 6, 2016

Is it possible to export Arrow functions in ES6/7?

Yes. export doesn't care about the value you want to export.

The export statement below gives a syntax error ... why?

You cannot have a default export and give it a name ("default" is already the name of the export).

Either do

export default () => console.log("say hello");

or

const hello = () => console.log("say hello");
export default hello;