Skip to content
This repository was archived by the owner on Sep 2, 2023. It is now read-only.
This repository was archived by the owner on Sep 2, 2023. It is now read-only.

Star exports from CJS should fail with a clear error (or at least warn) #545

@dfabulich

Description

@dfabulich

Consider this example:

// util.cjs
module.exports.sum = (x, y) => x + y;

// star.mjs
export * from './util.cjs';

// main.mjs
import {sum} from `./star.mjs`
console.log(sum(2,4));

When main.mjs tries to import {sum} from './star.mjs', it will fail. SyntaxError: The requested module './star.mjs' does not provide an export named 'sum'".

That's because the spec'd behavior when doing a star export from a module with only a default export is to export no names at all. If you import * as star from './star.mjs', you'll find that star is an empty module; there's no way for main.mjs to get sum back out of star.mjs without modifying star.mjs.

(That spec'd behavior seems kinda silly to me; I tip my hat to @guybedford's proposal to include the default export in export * from 'module')

But as long as "export no names" is the behavior, and as long as CJS only generates default exports and not named exports, I think it would be helpful to throw a clear error in this case. export * from 'util.cjs' can't really do anything useful, and we could serve users better by doing something more friendly.

Maybe something like this?

[CJS_STAR_EXPORT]: "export *" from a CommonJS module is not allowed. (CommonJS exports are computed at runtime, but ES module exports must be computed earlier, during the parse phase.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions