Reputation: 63
I've defined a function as below.
export default const myFunc = () => {
// Do stuff
}
However, ESLint complains about the const with
Parsing Error: Only expressions, functions or classes are allowed as the default export
Interestingly, ESLint has no complaints if I remove the default
keyword.
Why does it fail to parse the function?
Upvotes: 0
Views: 2678
Reputation: 370739
What follows export default
must be an expression or a function declaration. The declaration of a variable is not an expression; it's a standalone statement, so it's not permitted where you're trying to put it. (Similarly, if (const foo = 'bar')
is not permitted either; only an expression is permitted there.) It's not an ESLint issue, it's a JavaScript syntax issue.
Change your code to:
export default () => {
// Do stuff
};
If you need the function to be named as well so you can reference it elsewhere in the file, you'll have to name it and export in two steps (if it needs to be an arrow function):
const myFunc = () => {
// Do stuff
};
export default myFunc;
Named exports, on the other hand, permit assigning to variables while also exporting the variables, which is why export const ...
is permitted. (But you're not assigning to a standalone variable when you do export default
)
Upvotes: 4