Repository navigation
Importing default module fails when using ES6 syntax #3337
Description
Activity
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();
Reacted by Christian Rondeau, Ben van de Sande, Manuel Rauber, Attila Egyed, Dheeraj Bhaskar, Rick D, Andy Fleming, dev4201, tedwong, Paul Grime and 91 moreReacted by Tomáš Horáček, PendletonJones and trisraelReacted by Philipp Kretzschmar, jestcastro, Claudio Holanda Jr., Tobias Witt, Qiang Li, Hunter Beast, Jung Woo James Jeong, Jay Shah, greg, Xin Meng and 8 moreReacted by Xin Meng, Jack Fischer, Pkmmte Xeleon, Jaime Gómez, Roberto Novelo, Jamon Holmgren, James R Lowrey, Alex Filatov, Jérémy Fornarino, Fábio Bernardo and 2 moreThanks! Closing this.
I suggest making the existing
export =syntax compatible withexport default. Right now it doesn't seem possible to perform a default import of modules declared with the old syntax.Take the
tapemodule 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 useexport default tapeinstead ofexport = tapethen it works, but it will make the definition incompatible with typescript compilers < 1.5Reacted by Raine Revere, Oleg Shalygin, Timophey Molchanov, muzuiget, Ethan Resnick, Erik Schierboom, Tyler Cloutier, Tomáš Hübelbauer, Sayan Pal, AJ and 36 more+1
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)
- addedBy DesignDeprecated - use "Working as Intended" or "Design Limitation" insteadDeprecated - use "Working as Intended" or "Design Limitation" instead
on Jun 8, 2015 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.Reacted by PendletonJones, Michael サイトー 中村 Bashurov and Ryan PetschekSetting "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
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.game.tsdoes not have adefaultexport. if it did, it would have looked like:export default class Game {
Mohamed Hegazy (@mhegazy) I am not wanting to use a default export here.
Is it
Game.tsorgame.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.tsand the code is as you say it is, then it is unlikely that theerror TS1192actually 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.can you share your code then. the error is only shown if you have a default import.
Mohamed Hegazy (@mhegazy) sorry please bear with me, I need to upload my local code after fixing smth.
Mohamed Hegazy (@mhegazy) switching to tsc vs ts-loader+webpack has fixed it for me.
https://github.com/QuantumInformation/HTML5SpaceInvaders/blob/master/Main.js#L3- Reacted by Jake Archibald, Alexis Villegas Torres, kyasbal, Cosmin Pitu, Ben Creasy, Joshua Grosso, Aden Narkowicz, xcatliu, Riokai, Andrew and 11 moreReacted by AndrewReacted by Aden Narkowicz, Andrew, Beau Roberts and Clément DOUINReacted by ddernon and Nicolas Märchy
- locked and limited conversation to collaborators
on Jun 19, 2018
I have tried importing
fsmodule usingimport 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?