0

I defined a javascript file as below:

export default {
  parseOptions: {
    tolerant: true,
    raw: true,
    tokens: true,
    range: true,
    comment: true,
  },
  syntaxType: {
    callback: 'callback',
    promise: 'promise',
    await: 'await',
  },
};

I use below code to import it in other files:

import { syntaxType } from './options';
...
synaxType.callback

I get an error that synaxType is not defined when I use it. However if I change to below code it will work fine:

import options from './options';
options.synaxType.callback

I wonder what wrong with my previous import. Do I need to configure anything for that? Below is my babel configuration:

{
  "presets": [
    ["es2015"],
    "stage-0",
    "react",
    "react-boilerplate"
  ],

  "plugins": [
    "transform-decorators-legacy",
    "transform-class-properties",
    "transform-async-to-generator",
    "react-hot-loader/babel"
  ]
}
Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523

3 Answers3

2

Because that's how the default works, you don't actually need to assign a name to it. If you want to use curly braces, you must set up up without the default like:

export var options = {
  parseOptions: {
    tolerant: true,
    raw: true,
    tokens: true,
    range: true,
    comment: true,
  },
  syntaxType: {
    callback: 'callback',
    promise: 'promise',
    await: 'await',
  },
};

Then you can

import { options } from './options';
options.syntaxType

Or

export var syntaxType = {
  callback: 'callback',
  promise: 'promise',
  await: 'await',
}
A. L
  • 11,695
  • 23
  • 85
  • 163
2

The syntax

import options from './options';

Will presume options is the default exported object. That is why options.synaxType.callback works.

The syntax

import { syntaxType } from './options';

Needs syntaxType to have been an exported member, such as:

export const syntaxType = {
    callback: 'callback',
    promise: 'promise',
    await: 'await',
};

If the above construct were present at the module, then that syntax would work.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • Complete explanation and more info at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import – acdcjunior Sep 07 '17 at 00:51
0

There are two types of exports, default exports and named exports. Default exports don't require a name, whereas named exports do.


Here's how to export a default value

export default "apple";

Here's how to import a default value

import iCanNameThisWhateverSinceItsADefaultExport from "./apple_module";

Here's how to export a named value

export const apple = "apple";

Here's how to import a named value

export {apple} from "./apple_module";

If you want to have named access to syntaxType, you have two options:

If you leave it how it is, you simply need to change your import statement to:

import options from "./options";
const {syntaxType, parseOptions} = options;
// or access properties normally via (i.e.) options.syntaxType

Or you can make syntaxType and parseOptions named exports:

export const parseOptions = {
    tolerant: true,
    raw: true,
    tokens: true,
    range: true,
    comment: true,
  };

export const syntaxType = {
    callback: 'callback',
    promise: 'promise',
    await: 'await',
  };

And import them with

import {syntaxType, parseOptions} from "options";

It's simply a syntax issue. Once you understand the syntax, you'll should be good to go with imports/exports. Read all about import and export statements in the excellent MDN docs.

Govind Rai
  • 14,406
  • 9
  • 72
  • 83
  • 1
    Don't forget `import {apple as iCanNameThisWhateverIWantSinceItsAnAliasedNamedExport} …` :-) – Bergi Sep 07 '17 at 02:21