134

I have created a new module 'A' and trying to import it in another module called 'B'. I am getting this error on compiling that says

error TS1192: Module '" A.module"' has no default export

Can anyone please help on how to solve this error.

Jimmar
  • 4,194
  • 2
  • 28
  • 43
Abhi
  • 4,553
  • 4
  • 16
  • 22
  • Does this answer your question? [Module has not default export after converting to Typescript](https://stackoverflow.com/questions/55870154/module-has-not-default-export-after-converting-to-typescript) – Michael Freidgeim Sep 28 '20 at 23:20

9 Answers9

291

This was a simple error. It occurred due to the missing {} around the module name in the import statement itself.

starball
  • 20,030
  • 7
  • 43
  • 238
Abhi
  • 4,553
  • 4
  • 16
  • 22
  • 11
    jeez. thanks... just FYI, this is whats going on lol... WRONG: import YourWhateverThing from './your-whatever-file.component.' RIGHT: import {YourWhateverThing} from from './your-whatever-file.component.' – Andy Danger Gagne Jun 28 '17 at 15:06
  • Note: This answer was added at the very initial stages of Angular 2. Please check your version and solution accordingly, since this may not work for all the similar issues as mentioned by other kind people. Cheers. – Abhi Feb 21 '22 at 03:19
156

Accepted answer didn't work for me, so I am posting more info.

I had:

import Module from 'module';

and this worked for me:

import * as Module from 'module';

src: https://github.com/Microsoft/TypeScript/issues/3337#issuecomment-107971371

nothing-special-here
  • 11,230
  • 13
  • 64
  • 94
  • 3
    jeez, lucky I found this. Thanks guys! – Radityo Ardi Feb 04 '21 at 10:38
  • Thank you! To use import X from 'xPath', then there needs to be an export default. But if there is export default, then we use import * as X from 'xPath'. Wow! – Thuy Aug 26 '21 at 20:53
  • Oh wow I feel dumb. TS was complaining about not having default exports and this was the way to go, made them all named and then imported them all so i could do ComponentName.NamedComponent – Lauren Dec 01 '22 at 14:50
34

For me this issue was addressed using "allowSyntheticDefaultImports": true within compilerOptions in the project's tsconfig.json.

Our specific error related to a moment js import.

More about this option can be found here

Michael Hancock
  • 2,673
  • 1
  • 18
  • 37
  • 4
    This is the only correct option when using TypeScript to type-check plain JavaScript. If anyone else comes across this, remember to restart the language service (VS Code: Ctrl/Cmd+Shift+P > TypeScript: Restart TSServer) after updating tsconfig.json if the option does not seem to come into effect immediately. – Tomáš Hübelbauer Mar 28 '21 at 13:07
  • @TomášHübelbauer adding the key/value to compileroptions with restart of tsserver in vscode did not help, but [`import * as foo from 'bar'`](https://stackoverflow.com/a/50473239/1705829) helped. – Timo Aug 07 '22 at 06:44
  • 1
    This worked for me. Thanks. – Ronn Wilder Jun 15 '23 at 05:17
23

Use:

import { Module } from 'module';

You need to insert the module name between {...}

Rafael Tovar
  • 239
  • 2
  • 3
8

I fixed it by adding this to tsconfig.json in my angular project.

{
  ...
  "compilerOptions": {
    ...
    "allowSyntheticDefaultImports": true,
tbo47
  • 2,688
  • 3
  • 20
  • 11
4

I fixed above issue by putting the following inside the tsconfig.json file:

{
  "compilerOptions": {
    ...
    "esModuleInterop": true
    ...
  }
}

ziishaned
  • 4,944
  • 3
  • 25
  • 32
1

Instead of importing it as module, you can also require it like so:

const Module = require('./some_folder/module');

module.ts would then have:

module.exports = class Module {
    ... your module code
}
José Veríssimo
  • 221
  • 2
  • 11
1

All above answers didn't solve my error. Below solution worked for me. Before this, I have run below command

npm i @types/moment-timezone --save-dev

And then I imported moment like below in .ts file.

import * as moment from "moment-timezone";

I hope, it will help someone. Thanks!

Rajeev Kumar
  • 371
  • 2
  • 9
0

To solve this problem, instead of exporting the module as named export, we need to make it a default exported module as below

export default class Module {
    ... your module code
}