0

I am doing the following (both of the exported are objects):

SlateInfo.js

export default {SlateRules, SlateSchema};

and then in another file

import {SlateRules, SlateSchema} from 'SlateInfo';

But what I get is that SlateRules is defined as an object containing SlateRules and SlateSchema

SlateSchema is left undefined.

Where am I going wrong/what am I misunderstanding?

Slbox
  • 10,957
  • 15
  • 54
  • 106
  • Both `SlateRules` and `SlateSchema` should be `undefined` in your example, unless you export `SlateRules` as well somewhere. – Felix Kling Jan 05 '17 at 14:41
  • I get the same results if I use `export {SlateRules, SlateSchema}` where both exports end up imported into `SlateRules` – Slbox Jan 05 '17 at 14:45
  • Are you importing from the wrong module perhaps? If the exports are in your own file, it should be something like `... from './path/to/SlateInfo'`. – Felix Kling Jan 05 '17 at 14:47

1 Answers1

4

import {SlateRules, SlateSchema} from 'SlateInfo'; is for importing named exports. However, export default {SlateRules, SlateSchema}; is a default export, exporting an object with two properties.

If you want named exports then you need to omit the default:

export {SlateRules, SlateSchema};

Or use a default import and destructure the object:

import SlateInfo from 'SlateInfo';
const {SlateRules, SlateSchema} = SlateInfo;

When should I use curly braces for ES6 import? has a lot of examples showing how named and default exports work.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Thanks for your response. Unfortunately I tried it without default on the export and it results in the same thing. – Slbox Jan 05 '17 at 14:38
  • It didn't occur to me to put them into consts that way, but since I need these in a variety of files I'd love to avoid that method if possible. – Slbox Jan 05 '17 at 14:38
  • I also tried `export const SlateRules = {}` and `export const SlateSchema = {}` with the same results if I recall correctly. – Slbox Jan 05 '17 at 14:39
  • The value of `SlateRules` might already be an object with that properties, we don't know. Also make sure you are using the latest version of Babel. I can only answer based on the information you provide. – Felix Kling Jan 05 '17 at 14:40
  • I'm sorry I'm not sure what you mean when you say that it might already be an object with those properties. I am using the latest version of babel-core (6.21) – Slbox Jan 05 '17 at 14:42
  • If you had `var SlateRules = {SlateRules: ..., SlateSchema: ...}; export {SlateRules}`, you'd get the result you are seeing. Otherwise you should not. Without a complete example we can only speculate what else could be wrong. – Felix Kling Jan 05 '17 at 14:43
  • Unfortunately I don't have that anywhere, which is why I'm so baffled as to these results. – Slbox Jan 05 '17 at 14:44
  • I appreciate that fact. Your later points though are on code I have not posted. – Slbox Jan 05 '17 at 14:48