Skip to content

Importing default module fails when using ES6 syntax #3337

Description

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?

Activity

  1. ivogabe commented on Jun 2, 2015

    @ivogabe
    Contributor

    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();
  2. astoilkov commented on Jun 2, 2015

    @astoilkov
    Author

    Thanks! Closing this.

  3. tarruda commented on Jun 6, 2015

    @tarruda

    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

  4. astoilkov commented on Jun 6, 2015

    @astoilkov
    Author

    +1

  5. danquirk commented on Jun 8, 2015

    @danquirk
    Member

    We talked a lot about whether we can (or should) make export= and export default the same thing. You can see the discussion and conclusions here: #2242 (comment)

  6. added
    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead
    on Jun 8, 2015
  7. c9s commented on May 26, 2016

    @c9s

    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.
    
  8. c9s commented on May 26, 2016

    @c9s

    Setting "target" to "es5" and

    import fs = require('fs');
    // or this
    import assign = require('object-assign');
    

    will work just fine, however this syntax will cause error with target = es6

  9. quantuminformation commented on Jul 10, 2016

    @quantuminformation

    What is wrong with:

    import {Game} from "./Game";

    game.ts:

    export class Game {

    I get the error:

    error TS1192: Module '"lib/Game"' has no default export.

  10. mhegazy commented on Jul 11, 2016

    @mhegazy
    Contributor

    game.ts does not have a default export. if it did, it would have looked like:

    export default class Game {
  11. quantuminformation commented on Jul 12, 2016

    @quantuminformation

    Mohamed Hegazy (@mhegazy) I am not wanting to use a default export here.

  12. kitsonk commented on Jul 12, 2016

    @kitsonk
    Contributor

    Is it Game.ts or game.ts... While some file systems allow case insensitivity in filenames, it can cause all sorts of strange issues in certain situations.

    If it is Game.ts and the code is as you say it is, then it is unlikely that the error TS1192 actually refers to the code you are providing, as it should work and not generate the error your are encountering. It is likely something else is going on.

  13. mhegazy commented on Jul 12, 2016

    @mhegazy
    Contributor

    can you share your code then. the error is only shown if you have a default import.

  14. quantuminformation commented on Jul 13, 2016

    @quantuminformation

    Mohamed Hegazy (@mhegazy) sorry please bear with me, I need to upload my local code after fixing smth.

  15. quantuminformation commented on Jul 14, 2016

    @quantuminformation
  16. PendletonJones commented on Jun 10, 2017

    @PendletonJones

    --allowSyntheticDefaultImports solved this for me, #2242 #5577

  17. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions