1

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 ?

Dhiral Kaniya
  • 1,941
  • 1
  • 19
  • 32

1 Answers1

3

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.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Thanks for clarification, don't you think recommendations for constant export should be improve at this point ? – Dhiral Kaniya May 23 '19 at 08:21
  • What do you mean, "should be improve"? The error is *pretty* self-explanatory already, what were you thinking of? – CertainPerformance May 23 '19 at 08:23
  • As per error, if i export with like `export default const myObject` then another rule has been violated and it says "export default is use with classes or function only". So here, i would suggesting to improve error message for constant default if possible. – Dhiral Kaniya May 23 '19 at 08:26
  • 1
    Yeah, though that's not really a linting rule, but just how the syntax is required to be *everywhere*, see the link in the answer. I don't know the reason for it either, seems a bit strange – CertainPerformance May 23 '19 at 08:28