Dhiral Kaniya
Dhiral Kaniya

Reputation: 2051

Prefer default export.eslint(import/prefer-default-export) for constant

I am dealing with constant file and having just one constant object in that file. I am facing trouble with ESLINT error checking part.

Constant file name :- constant.js

export const myObject = {
    const1:'hello world',
    const2:'new world'
}

Getting an eslint error Prefer default export.eslint(import/prefer-default-export)

Here constants are not allow to export default( only class and function are allow to export)

Environment information

ESLint plugin version :- 1.9.0

Here, how do i create and export constant without eslint error ?

Upvotes: 1

Views: 562

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 371233

Either define the object up front, and then export it:

const myObject = {
  const1:'hello world',
  const2:'new world'
};
export default myObject;

Or just export the object expression:

export default {
  const1:'hello world',
  const2:'new world'
};

The second case doesn't have const anywhere, but the default export isn't reassignable anyway.

Here constants are not allow to export default( only class and function are allow to export)

The problem was not that you can't export default a const (though you can't do so in one line), but that the linting rule is pushing you to actually use export default (rather than a named export, as you were doing), since you only have a single export.

Upvotes: 3

Related Questions