Esm: Named exports with export default is not working

Created on 8 Dec 2017  路  7Comments  路  Source: standard-things/esm

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?

invalid

Most helpful comment

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;

All 7 comments

Hi @eduardojmatos,

Can you create a small repro repo?

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;

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.

Thanks @tilgovi, you're totally right!

@jdalton thanks for support!

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'

=)

@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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pvdlg picture pvdlg  路  3Comments

bensampaio picture bensampaio  路  3Comments

greggb picture greggb  路  3Comments

tunnckoCore picture tunnckoCore  路  3Comments

MVSICA-FICTA picture MVSICA-FICTA  路  3Comments