Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing default module fails when using ES6 syntax #3337

Closed
astoilkov opened this issue Jun 2, 2015 · 16 comments
Closed

Importing default module fails when using ES6 syntax #3337

astoilkov opened this issue Jun 2, 2015 · 16 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@astoilkov
Copy link

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?

@ivogabe
Copy link
Contributor

ivogabe commented Jun 2, 2015

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();

@astoilkov
Copy link
Author

Thanks! Closing this.

@tarruda
Copy link

tarruda commented Jun 6, 2015

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

@astoilkov
Copy link
Author

+1

@astoilkov astoilkov reopened this Jun 6, 2015
@danquirk
Copy link
Member

danquirk commented Jun 8, 2015

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)

@danquirk danquirk closed this as completed Jun 8, 2015
@danquirk danquirk added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Jun 8, 2015
@c9s
Copy link

c9s commented May 26, 2016

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.

@c9s
Copy link

c9s commented May 26, 2016

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

@quantuminformation
Copy link

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.

@mhegazy
Copy link
Contributor

mhegazy commented Jul 11, 2016

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

export default class Game {

@quantuminformation
Copy link

@mhegazy I am not wanting to use a default export here.

@kitsonk
Copy link
Contributor

kitsonk commented Jul 12, 2016

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.

@mhegazy
Copy link
Contributor

mhegazy commented Jul 12, 2016

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

@quantuminformation
Copy link

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

@quantuminformation
Copy link

@mhegazy switching to tsc vs ts-loader+webpack has fixed it for me.
https://github.com/QuantumInformation/HTML5SpaceInvaders/blob/master/Main.js#L3

@PendletonJones
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

9 participants