Skip to content

ES6 Modules default exports interop with CommonJS #2719

Closed
@teppeis

Description

@teppeis

CommonJS cannot requires a package transpiled from TypeScript ES6 Modules using default export well.

TS 1.5-alpha transpiles default exports:

// foo.ts
export default foo;

to:

// foo.js ("main" in this package.json)
exports.default = foo;

So users of this package foo need to add .default property to require().

var foo = require('foo').default;

It's a little messy...

Babel resolves this issue.

http://babeljs.io/docs/usage/modules/#interop

Interop

In order to encourage the use of CommonJS and ES6 modules, when exporting a default
export with no other exports module.exports will be set in addition to exports["default"].

export default test;
exports["default"] = test;
module.exports = exports["default"];

If you don't want this behaviour then you can use the commonStrict module formatter.

It sounds pretty nice and actually works for me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    CommittedThe team has roadmapped this issueFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions