Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named exports with export default is not working #182

Closed
eduardojmatos opened this issue Dec 8, 2017 · 7 comments
Closed

Named exports with export default is not working #182

eduardojmatos opened this issue Dec 8, 2017 · 7 comments
Labels

Comments

@eduardojmatos
Copy link

Hi folks! First, thanks for this awesome project!

I'm using @std/esm on my app and I have an issue to report.

The sample of the issue is very simple:

// dependency.js
export default const a = 1;
export const b = 2;
// main file
import { a, b } from './dependency';

This causes this error:

SyntaxError: Module '/Users/me/project-path/dependency.js' does not provide an export named 'a'`

To avoid this I'm removing the default option from module:

// dependency.js
export const a = 1;
export const b = 2;

And on this way, all is fine. Do am I losting some thing or this is really an issue?

@jdalton
Copy link
Member

jdalton commented Dec 8, 2017

Hi @eduardojmatos,

Can you create a small repro repo?

@eduardojmatos
Copy link
Author

@jdalton yes, for sure!

https://github.com/eduardojmatos/esm-sample/tree/master

@tilgovi
Copy link
Contributor

tilgovi commented Dec 8, 2017

I think this is expected behavior of ES modules. export default does not create a named export.

Use this to get what you want:

export const a = 1;
export const b = 2;
export default a;

@jdalton
Copy link
Member

jdalton commented Dec 8, 2017

Yep, @tilgovi is right.

You're getting the error because you're not exporting the a binding as a.

I renamed the files to .mjs and ran it with the --experimental-modules flag and got the same error:

(node:80495) ExperimentalWarning: The ESM module loader is experimental.
SyntaxError: The requested module does not provide an export named 'a'
    at ModuleJob._instantiate (internal/loader/ModuleJob.js:84:17)

However, if the code is changed to @tilgovi's suggestion then it works.

@jdalton jdalton closed this as completed Dec 8, 2017
@jdalton jdalton added the invalid label Dec 8, 2017
@eduardojmatos
Copy link
Author

Thanks @tilgovi, you're totally right!

@jdalton thanks for support!

@fdaciuk
Copy link

fdaciuk commented Dec 9, 2017

Just to complement: you might name default exported as a when you are importing:

import { default as a, b } from './dependency'

Or:

import a, { b } from './dependency'

=)

@eduardojmatos
Copy link
Author

@fdaciuk yes, but in this case, the problem was the default export. When I changed the way that I was exporting, even this sample that you explained, did works =D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants