Difference between ES2015 `import * as` vs just plain `import`
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 newfold
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
add a comment |
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 newfold
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
add a comment |
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 newfold
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
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 newfold
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
import module coffeescript ecmascript-6
asked Oct 27 '16 at 14:51
LeftiumLeftium
7,31754370
7,31754370
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
.
Should probably changesomething.default
tosomething['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
add a comment |
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.
Is it normal for there to be aCodeMirror.default
property as a result ofimport * as CodeMirror from 'codemirror'
? I don't thinkcodemirror
explicitly named an export called "default"
– Leftium
Oct 27 '16 at 15:19
They probably have a default export marked asexport default <whatever>
, and I think when you do a wildcard import, that comes in under the keydefault
.
– Joe Attardi
Oct 27 '16 at 15:21
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
.
Should probably changesomething.default
tosomething['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
add a comment |
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
.
Should probably changesomething.default
tosomething['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
add a comment |
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
.
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
.
edited Nov 14 '18 at 12:05
answered Oct 27 '16 at 15:10
user3568791user3568791
113110
113110
Should probably changesomething.default
tosomething['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
add a comment |
Should probably changesomething.default
tosomething['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
add a comment |
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.
Is it normal for there to be aCodeMirror.default
property as a result ofimport * as CodeMirror from 'codemirror'
? I don't thinkcodemirror
explicitly named an export called "default"
– Leftium
Oct 27 '16 at 15:19
They probably have a default export marked asexport default <whatever>
, and I think when you do a wildcard import, that comes in under the keydefault
.
– Joe Attardi
Oct 27 '16 at 15:21
add a comment |
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.
Is it normal for there to be aCodeMirror.default
property as a result ofimport * as CodeMirror from 'codemirror'
? I don't thinkcodemirror
explicitly named an export called "default"
– Leftium
Oct 27 '16 at 15:19
They probably have a default export marked asexport default <whatever>
, and I think when you do a wildcard import, that comes in under the keydefault
.
– Joe Attardi
Oct 27 '16 at 15:21
add a comment |
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.
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.
answered Oct 27 '16 at 15:09
Joe AttardiJoe Attardi
2,40112127
2,40112127
Is it normal for there to be aCodeMirror.default
property as a result ofimport * as CodeMirror from 'codemirror'
? I don't thinkcodemirror
explicitly named an export called "default"
– Leftium
Oct 27 '16 at 15:19
They probably have a default export marked asexport default <whatever>
, and I think when you do a wildcard import, that comes in under the keydefault
.
– Joe Attardi
Oct 27 '16 at 15:21
add a comment |
Is it normal for there to be aCodeMirror.default
property as a result ofimport * as CodeMirror from 'codemirror'
? I don't thinkcodemirror
explicitly named an export called "default"
– Leftium
Oct 27 '16 at 15:19
They probably have a default export marked asexport default <whatever>
, and I think when you do a wildcard import, that comes in under the keydefault
.
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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