Difference between ES2015 `import * as` vs just plain `import`










2















I just fixed a bug by changing import * as CodeMirror to just plain import CodeMirror.



  • I copied this code. (Porting it from TypeScript)


  • import * as CodeMirror worked until an addon was imported for its side effects: the expected new fold property was undefined.

Questions: (I am trying to understand what happened better)



  • What is going on? How did this change fix the bug?

  • Who is adding the default property to CodeMirror? (Or more likely: wrapping the module inside another object that looks very similar) The most likely suspects:

    • JavaScript modules (ES2015)

    • Babel

    • Webpack

    • CoffeeScript

    • CodeMirror


  • Is there a better way to accomplish what I was trying to achieve?


More details:



This code doesn't work as expected:



import * as CodeMirror from 'codemirror'
import 'codemirror/addon/fold/indent-fold.js' # should add `fold` object to `CodeMirror`

console.log typeof CodeMirror ## 'object'
console.log typeof CodeMirror.fold ## 'undefined'
console.log typeof CodeMirror.default ## 'function'

## Work-around:
console.log typeof CodeMirror.default.fold ## 'object'


This code works as expected:



import CodeMirror from 'codemirror'
import 'codemirror/addon/fold/indent-fold.js' # should add `fold` object to `CodeMirror`

console.log typeof CodeMirror ## 'function'
console.log typeof CodeMirror.fold ## 'object'
console.log typeof CodeMirror.default ## 'undefined'



I have already studied these resources, but they have not helped me fully understand what happened:



  • JS import reference

  • JS export reference

  • CoffeeScript modules









share|improve this question


























    2















    I just fixed a bug by changing import * as CodeMirror to just plain import CodeMirror.



    • I copied this code. (Porting it from TypeScript)


    • import * as CodeMirror worked until an addon was imported for its side effects: the expected new fold property was undefined.

    Questions: (I am trying to understand what happened better)



    • What is going on? How did this change fix the bug?

    • Who is adding the default property to CodeMirror? (Or more likely: wrapping the module inside another object that looks very similar) The most likely suspects:

      • JavaScript modules (ES2015)

      • Babel

      • Webpack

      • CoffeeScript

      • CodeMirror


    • Is there a better way to accomplish what I was trying to achieve?


    More details:



    This code doesn't work as expected:



    import * as CodeMirror from 'codemirror'
    import 'codemirror/addon/fold/indent-fold.js' # should add `fold` object to `CodeMirror`

    console.log typeof CodeMirror ## 'object'
    console.log typeof CodeMirror.fold ## 'undefined'
    console.log typeof CodeMirror.default ## 'function'

    ## Work-around:
    console.log typeof CodeMirror.default.fold ## 'object'


    This code works as expected:



    import CodeMirror from 'codemirror'
    import 'codemirror/addon/fold/indent-fold.js' # should add `fold` object to `CodeMirror`

    console.log typeof CodeMirror ## 'function'
    console.log typeof CodeMirror.fold ## 'object'
    console.log typeof CodeMirror.default ## 'undefined'



    I have already studied these resources, but they have not helped me fully understand what happened:



    • JS import reference

    • JS export reference

    • CoffeeScript modules









    share|improve this question
























      2












      2








      2


      0






      I just fixed a bug by changing import * as CodeMirror to just plain import CodeMirror.



      • I copied this code. (Porting it from TypeScript)


      • import * as CodeMirror worked until an addon was imported for its side effects: the expected new fold property was undefined.

      Questions: (I am trying to understand what happened better)



      • What is going on? How did this change fix the bug?

      • Who is adding the default property to CodeMirror? (Or more likely: wrapping the module inside another object that looks very similar) The most likely suspects:

        • JavaScript modules (ES2015)

        • Babel

        • Webpack

        • CoffeeScript

        • CodeMirror


      • Is there a better way to accomplish what I was trying to achieve?


      More details:



      This code doesn't work as expected:



      import * as CodeMirror from 'codemirror'
      import 'codemirror/addon/fold/indent-fold.js' # should add `fold` object to `CodeMirror`

      console.log typeof CodeMirror ## 'object'
      console.log typeof CodeMirror.fold ## 'undefined'
      console.log typeof CodeMirror.default ## 'function'

      ## Work-around:
      console.log typeof CodeMirror.default.fold ## 'object'


      This code works as expected:



      import CodeMirror from 'codemirror'
      import 'codemirror/addon/fold/indent-fold.js' # should add `fold` object to `CodeMirror`

      console.log typeof CodeMirror ## 'function'
      console.log typeof CodeMirror.fold ## 'object'
      console.log typeof CodeMirror.default ## 'undefined'



      I have already studied these resources, but they have not helped me fully understand what happened:



      • JS import reference

      • JS export reference

      • CoffeeScript modules









      share|improve this question














      I just fixed a bug by changing import * as CodeMirror to just plain import CodeMirror.



      • I copied this code. (Porting it from TypeScript)


      • import * as CodeMirror worked until an addon was imported for its side effects: the expected new fold property was undefined.

      Questions: (I am trying to understand what happened better)



      • What is going on? How did this change fix the bug?

      • Who is adding the default property to CodeMirror? (Or more likely: wrapping the module inside another object that looks very similar) The most likely suspects:

        • JavaScript modules (ES2015)

        • Babel

        • Webpack

        • CoffeeScript

        • CodeMirror


      • Is there a better way to accomplish what I was trying to achieve?


      More details:



      This code doesn't work as expected:



      import * as CodeMirror from 'codemirror'
      import 'codemirror/addon/fold/indent-fold.js' # should add `fold` object to `CodeMirror`

      console.log typeof CodeMirror ## 'object'
      console.log typeof CodeMirror.fold ## 'undefined'
      console.log typeof CodeMirror.default ## 'function'

      ## Work-around:
      console.log typeof CodeMirror.default.fold ## 'object'


      This code works as expected:



      import CodeMirror from 'codemirror'
      import 'codemirror/addon/fold/indent-fold.js' # should add `fold` object to `CodeMirror`

      console.log typeof CodeMirror ## 'function'
      console.log typeof CodeMirror.fold ## 'object'
      console.log typeof CodeMirror.default ## 'undefined'



      I have already studied these resources, but they have not helped me fully understand what happened:



      • JS import reference

      • JS export reference

      • CoffeeScript modules






      import module coffeescript ecmascript-6






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 27 '16 at 14:51









      LeftiumLeftium

      7,31754370




      7,31754370






















          2 Answers
          2






          active

          oldest

          votes


















          4














          Let's assume you have a very simple module named 'test-module', in it you have:



          var test = 'test';
          export default test;
          export function helloWorld () ... ;


          When you do:



          import something from 'test-module';


          you are only importing the default export of 'some-module'. In this case, it is the string test. The default export can be anything, object, function, etc.



          When you do:



          import helloWorld from 'test-module';


          You are specifically importing a member of 'test-module' named 'helloWorld' and not the default export. In this case, it is the function 'helloWorld'.



          If you had done:



          import something from 'test-module';


          The 'something' would be 'undefined' since there is no export for with that name.



          import * as something from 'test-module';


          is asking for an object with all of the named exports of 'test-module'.



          Then you can access any of the exports in 'test-module' as something.name. In this case they will be something.default and something.helloWorld.






          share|improve this answer

























          • Should probably change something.default to something['default'] (just like with all keyword properties) as not all transpilers will do this for you and it will error on older browsers.

            – Jared Smith
            Oct 28 '16 at 13:13



















          0














          import * as CodeMirror from 'codemirror' imports all the named exports and namespaces them into an object called CodeMirror.



          import CodeMirror from 'codemirror' imports the default export.






          share|improve this answer























          • Is it normal for there to be a CodeMirror.default property as a result ofimport * as CodeMirror from 'codemirror'? I don't think codemirror explicitly named an export called "default"

            – Leftium
            Oct 27 '16 at 15:19












          • They probably have a default export marked as export default <whatever>, and I think when you do a wildcard import, that comes in under the key default.

            – Joe Attardi
            Oct 27 '16 at 15:21










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40287795%2fdifference-between-es2015-import-as-vs-just-plain-import%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          Let's assume you have a very simple module named 'test-module', in it you have:



          var test = 'test';
          export default test;
          export function helloWorld () ... ;


          When you do:



          import something from 'test-module';


          you are only importing the default export of 'some-module'. In this case, it is the string test. The default export can be anything, object, function, etc.



          When you do:



          import helloWorld from 'test-module';


          You are specifically importing a member of 'test-module' named 'helloWorld' and not the default export. In this case, it is the function 'helloWorld'.



          If you had done:



          import something from 'test-module';


          The 'something' would be 'undefined' since there is no export for with that name.



          import * as something from 'test-module';


          is asking for an object with all of the named exports of 'test-module'.



          Then you can access any of the exports in 'test-module' as something.name. In this case they will be something.default and something.helloWorld.






          share|improve this answer

























          • Should probably change something.default to something['default'] (just like with all keyword properties) as not all transpilers will do this for you and it will error on older browsers.

            – Jared Smith
            Oct 28 '16 at 13:13
















          4














          Let's assume you have a very simple module named 'test-module', in it you have:



          var test = 'test';
          export default test;
          export function helloWorld () ... ;


          When you do:



          import something from 'test-module';


          you are only importing the default export of 'some-module'. In this case, it is the string test. The default export can be anything, object, function, etc.



          When you do:



          import helloWorld from 'test-module';


          You are specifically importing a member of 'test-module' named 'helloWorld' and not the default export. In this case, it is the function 'helloWorld'.



          If you had done:



          import something from 'test-module';


          The 'something' would be 'undefined' since there is no export for with that name.



          import * as something from 'test-module';


          is asking for an object with all of the named exports of 'test-module'.



          Then you can access any of the exports in 'test-module' as something.name. In this case they will be something.default and something.helloWorld.






          share|improve this answer

























          • Should probably change something.default to something['default'] (just like with all keyword properties) as not all transpilers will do this for you and it will error on older browsers.

            – Jared Smith
            Oct 28 '16 at 13:13














          4












          4








          4







          Let's assume you have a very simple module named 'test-module', in it you have:



          var test = 'test';
          export default test;
          export function helloWorld () ... ;


          When you do:



          import something from 'test-module';


          you are only importing the default export of 'some-module'. In this case, it is the string test. The default export can be anything, object, function, etc.



          When you do:



          import helloWorld from 'test-module';


          You are specifically importing a member of 'test-module' named 'helloWorld' and not the default export. In this case, it is the function 'helloWorld'.



          If you had done:



          import something from 'test-module';


          The 'something' would be 'undefined' since there is no export for with that name.



          import * as something from 'test-module';


          is asking for an object with all of the named exports of 'test-module'.



          Then you can access any of the exports in 'test-module' as something.name. In this case they will be something.default and something.helloWorld.






          share|improve this answer















          Let's assume you have a very simple module named 'test-module', in it you have:



          var test = 'test';
          export default test;
          export function helloWorld () ... ;


          When you do:



          import something from 'test-module';


          you are only importing the default export of 'some-module'. In this case, it is the string test. The default export can be anything, object, function, etc.



          When you do:



          import helloWorld from 'test-module';


          You are specifically importing a member of 'test-module' named 'helloWorld' and not the default export. In this case, it is the function 'helloWorld'.



          If you had done:



          import something from 'test-module';


          The 'something' would be 'undefined' since there is no export for with that name.



          import * as something from 'test-module';


          is asking for an object with all of the named exports of 'test-module'.



          Then you can access any of the exports in 'test-module' as something.name. In this case they will be something.default and something.helloWorld.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 12:05

























          answered Oct 27 '16 at 15:10









          user3568791user3568791

          113110




          113110












          • Should probably change something.default to something['default'] (just like with all keyword properties) as not all transpilers will do this for you and it will error on older browsers.

            – Jared Smith
            Oct 28 '16 at 13:13


















          • Should probably change something.default to something['default'] (just like with all keyword properties) as not all transpilers will do this for you and it will error on older browsers.

            – Jared Smith
            Oct 28 '16 at 13:13

















          Should probably change something.default to something['default'] (just like with all keyword properties) as not all transpilers will do this for you and it will error on older browsers.

          – Jared Smith
          Oct 28 '16 at 13:13






          Should probably change something.default to something['default'] (just like with all keyword properties) as not all transpilers will do this for you and it will error on older browsers.

          – Jared Smith
          Oct 28 '16 at 13:13














          0














          import * as CodeMirror from 'codemirror' imports all the named exports and namespaces them into an object called CodeMirror.



          import CodeMirror from 'codemirror' imports the default export.






          share|improve this answer























          • Is it normal for there to be a CodeMirror.default property as a result ofimport * as CodeMirror from 'codemirror'? I don't think codemirror explicitly named an export called "default"

            – Leftium
            Oct 27 '16 at 15:19












          • They probably have a default export marked as export default <whatever>, and I think when you do a wildcard import, that comes in under the key default.

            – Joe Attardi
            Oct 27 '16 at 15:21















          0














          import * as CodeMirror from 'codemirror' imports all the named exports and namespaces them into an object called CodeMirror.



          import CodeMirror from 'codemirror' imports the default export.






          share|improve this answer























          • Is it normal for there to be a CodeMirror.default property as a result ofimport * as CodeMirror from 'codemirror'? I don't think codemirror explicitly named an export called "default"

            – Leftium
            Oct 27 '16 at 15:19












          • They probably have a default export marked as export default <whatever>, and I think when you do a wildcard import, that comes in under the key default.

            – Joe Attardi
            Oct 27 '16 at 15:21













          0












          0








          0







          import * as CodeMirror from 'codemirror' imports all the named exports and namespaces them into an object called CodeMirror.



          import CodeMirror from 'codemirror' imports the default export.






          share|improve this answer













          import * as CodeMirror from 'codemirror' imports all the named exports and namespaces them into an object called CodeMirror.



          import CodeMirror from 'codemirror' imports the default export.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 27 '16 at 15:09









          Joe AttardiJoe Attardi

          2,40112127




          2,40112127












          • Is it normal for there to be a CodeMirror.default property as a result ofimport * as CodeMirror from 'codemirror'? I don't think codemirror explicitly named an export called "default"

            – Leftium
            Oct 27 '16 at 15:19












          • They probably have a default export marked as export default <whatever>, and I think when you do a wildcard import, that comes in under the key default.

            – Joe Attardi
            Oct 27 '16 at 15:21

















          • Is it normal for there to be a CodeMirror.default property as a result ofimport * as CodeMirror from 'codemirror'? I don't think codemirror explicitly named an export called "default"

            – Leftium
            Oct 27 '16 at 15:19












          • They probably have a default export marked as export default <whatever>, and I think when you do a wildcard import, that comes in under the key default.

            – Joe Attardi
            Oct 27 '16 at 15:21
















          Is it normal for there to be a CodeMirror.default property as a result ofimport * as CodeMirror from 'codemirror'? I don't think codemirror explicitly named an export called "default"

          – Leftium
          Oct 27 '16 at 15:19






          Is it normal for there to be a CodeMirror.default property as a result ofimport * as CodeMirror from 'codemirror'? I don't think codemirror explicitly named an export called "default"

          – Leftium
          Oct 27 '16 at 15:19














          They probably have a default export marked as export default <whatever>, and I think when you do a wildcard import, that comes in under the key default.

          – Joe Attardi
          Oct 27 '16 at 15:21





          They probably have a default export marked as export default <whatever>, and I think when you do a wildcard import, that comes in under the key default.

          – Joe Attardi
          Oct 27 '16 at 15:21

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40287795%2fdifference-between-es2015-import-as-vs-just-plain-import%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto