1

In the following code:

ROOTNS.ui.components.orgChart = (function () {

    import PubSub from 'pubsub-js'

})();

I get the runtime error on the import statement of:

Uncaught SyntaxError: Unexpected identifier

My app is based on Chromium version 66, which supports the import statement, so what is wrong with the syntax?

7
  • 2
    import needs to be at the top of your unit. Commented Jan 28, 2019 at 10:16
  • But that would place it outside of the closure. Is that not a problem if I have multiple modules in my app that also use the same import statement? Commented Jan 28, 2019 at 10:17
  • 1
    As far as I know, unless your using a dynamic import, you need your imports at the top of the unit. Why does PubSub need to be inside the closure.? Commented Jan 28, 2019 at 10:18
  • 1
    It will only get imported once, that's what module loaders job is. Commented Jan 28, 2019 at 10:23
  • 1
    @AndroidDev No, there's no conflict, each module has its own scope. You don't need IIFEs when using ES6 modules. Commented Jan 28, 2019 at 10:23

1 Answer 1

2

Imports must be at the top of your script before any other code. ES6 modules don't work like other module systems where you can load modules conditionally.

import PubSub from 'pubsub-js'

ROOTNS.ui.components.orgChart = (function () {

// other code 

})();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.