0

I am trying to set up a simple index file for all of my models. Ultimately, my goal would be to do something like this...

// models/A.model.js
...
const A = model(...);
const otherA = ...;
export { A, otherA };

// models/B.model.js
...
const B = model(...);
const otherB = ...
export { B, otherB };

// models/index.js
export * from './A.model.js';
export * from './B.model.js';

// server/index.js
import * as models from './models'
models.A
models.otherA

etc.

Currently it appears as if the models/index.js file is not exporting anything. I may be doing this completely wrong. I am using Babel and have used this as a reference

dmoree
  • 1
  • 2
  • @Phil I don't think this is a duplicate – Bergi Dec 08 '17 at 02:27
  • 1
    This should work. What makes it appear as if index.js is not exporting anything? What error(s) are you getting? – Bergi Dec 08 '17 at 02:28
  • @Bergi oops, I closed with the wrong duplicate. Was meant to be [import modules from files in directory](https://stackoverflow.com/questions/29722270/import-modules-from-files-in-directory). Can re-open if it appears OP has a different problem – Phil Dec 08 '17 at 02:43
  • @Phil If I re-opened I'd have to vote to close as not reproducable or missing information :-) Let's wait for clarification from the OP – Bergi Dec 08 '17 at 02:58
  • @Bergi There are no errors, but with the code as written gives an empty object as models in server/index.js, where I would expect to be able to access models.A and models.otherA, etc. – dmoree Dec 08 '17 at 23:20
  • How are you running the code, are you using a transpiler? What's the exact code you are using, can you make a [mcve]? – Bergi Dec 08 '17 at 23:21
  • @Phil I have seen those other questions, but it appears as if [here](https://stackoverflow.com/questions/41236920/es6-how-to-export-all-item-from-one-file) they are trying to export all the named exports from one file as a default for another. They never really explain anything after that. From the first [duplicate](https://stackoverflow.com/questions/29722270/import-modules-from-files-in-directory) they are importing the named exports of one file through an index. This also doesn't work for me. – dmoree Dec 08 '17 at 23:33
  • @Bergi This is part of a simple express app. I am using webpack to bundle and using 'babel-loader' to transpile the code. I apologize for the brevity. I am not an expert by any means. The problem appears to be that models/index.js is not exporting anything. – dmoree Dec 08 '17 at 23:43
  • Can you post the transpiled code? – Bergi Dec 08 '17 at 23:48
  • 1
    @Bergi From looking at the transpiled code the problem actually comes from the transform-runtime plugin injecting import statements when using 'export * from' see [#2877](https://github.com/babel/babel/issues/2877). Thank you for your help. – dmoree Dec 16 '17 at 00:51

0 Answers0