0

I'm having an issue using module.exports = {} in one of my project files.

The file contains many functions which are exported as named exports, but also contains an object spread. It looks something like this:

module.exports = {
    ...DEFAULT_METHODS,
    methodDefinedAbove,
    anotherFunction,
    moreFunctionsStill,
}

When I import from this file elsewhere in the project. The imports work fine. I can import both statically named functions, like :

import { methodDefinedAbove } from '../../theFileWithExports';

as well as functions that are defined in the DEFAULT_METHODS object, like :

import { definedInDefaultMethods } from '../../theFileWithExports';

This works fine in dozens of files in my project. Today I added a new file and the import does not work. When I remove the ...DEFAULT_METHODS spread, it does work. But I can't figure out why this works in dozens of places and would just break for a new file. The new file is a sibling of other files with working imports.

I can even hack a workaround in by importing the function I want in another file and saving it as a static class variable, then importing that class into the component and using it's static method.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
gvfordo
  • 189
  • 1
  • 10
  • 1
    Can you show us how you import it in case it is broken? What are you doing differently from other source files? – Al.G. Sep 07 '18 at 19:58
  • 2
    FYI, combining `module.exports` and named ES imports is a recipe for future broken code. There is no official specification for interop like this. It would be much better to use ES `export`. – loganfsmyth Sep 07 '18 at 20:03
  • I can only second loganfsmyth. Use proper ES6 module `export` syntax instead of `module.exports`. – Bergi Sep 07 '18 at 21:57
  • 1
    [`...` is not an operator!](https://stackoverflow.com/questions/37151966/what-is-spreadelement-in-ecmascript-documentation-is-it-the-same-as-spread-oper/37152508#37152508) – Felix Kling Sep 08 '18 at 00:53

0 Answers0