TypeScript: Importing default module fails when using ES6 syntax

I have tried importing fs module using import fs from 'fs' but this gives an error: `Module ‘path’ has no default export.'.

import fs = require('fs'); works but this is not ES6 syntax. I would like to stick with ES6 syntax because it is getting more and more popular and therefore easier to understand.

Are there any plans for supporting ES6 modules that import the entire TypeScript module?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 3
  • Comments: 16 (5 by maintainers)

Commits related to this issue

Most upvoted comments

You should use

import * as fs from 'fs';

You can use your syntax if the module has a default export. Example:

// foo.ts
export default class Foo {
}

// bar.ts
import Foo from './foo';
new Foo();

I suggest making the existing export = syntax compatible with export default. Right now it doesn’t seem possible to perform a default import of modules declared with the old syntax.

Take the tape module for example. I can’t import it using es6 syntax and the existing definition file. The following directive:

import tape from 'tape'

Fails with TS1192: External module ''tape'' has no default export.. If I change the definition file to use export default tape instead of export = tape then it works, but it will make the definition incompatible with typescript compilers < 1.5

–allowSyntheticDefaultImports solved this for me, #2242 #5577

I used

import * as assign from "object-assign";

And the error message becomes:

error TS2497: Module ''object-assign'' resolves to a non-module entity and cannot be imported using this construct.