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

Comments

@astoilkov
Copy link

commented Jun 2, 2015

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Author

commented Jun 2, 2015

Thanks! Closing this.

@astoilkov astoilkov closed this Jun 2, 2015
@tarruda

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Author

commented Jun 6, 2015

+1

@astoilkov astoilkov reopened this Jun 6, 2015
@danquirk

This comment has been minimized.

Copy link
Member

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)

@c9s

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Jul 10, 2016

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Jul 12, 2016

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

@kitsonk

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link

commented Jul 12, 2016

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

@QuantumInformation

This comment has been minimized.

Copy link

commented Jul 13, 2016

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

@QuantumInformation

This comment has been minimized.

Copy link

commented Jul 14, 2016

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

@PendletonJones

This comment has been minimized.

Copy link

commented Jun 10, 2017

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
9 participants
You can’t perform that action at this time.