Skip to content

Importing commonJS modules with es6 imports #11179

Closed
@testerez

Description

@testerez

I'm trying to convert a Babel project with typescript and I realize that I need to change some of the imports from import module from 'module' to import * as module from 'module' to make it work.
I understand that the modules I try to import has no "default" export but Babel deals with it by wrapping the import with _interopRequireDefault.

For example

import myModule from 'module';
console.log(myModule);

Is converted like this by Babel

'use strict';

var _module = require('module');

var _module2 = _interopRequireDefault(_module);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

console.log(_module2.default);

and like this by Typescript

"use strict";
var module_1 = require('module');
console.log(module_1.default);

Which logs undefined when importing a classic commonJS module.

Maybe Typescript is right here but the glue Babel provides appends to be pretty convenient. And overall, the difference in the typescript behaviour makes the conversion of an existing Babel project very painful.

Do you have any suggestion to make it easier?

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions