83

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
  • 3,011
  • 1
  • 24
  • 38
Abhi
  • 3,387
  • 3
  • 12
  • 19
  • 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

7 Answers7

228

This was a simple error. It occurred due to the missing {} around the module name in the import statement itself. Since this killed 2-3 hours of my time, sharing with you all so you won't waste yours. Hope this can help someone. Cheers, Abhi.

Abhi
  • 3,387
  • 3
  • 12
  • 19
  • 8
    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
71

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
  • 8,238
  • 10
  • 56
  • 90
19

Use:

import { Module } from 'module';

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

Rafael Tovar
  • 199
  • 1
  • 2
13

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,260
  • 1
  • 17
  • 32
  • 2
    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
  • 1
    This should be the accepted answer – johnmikelridzz May 15 '21 at 17:04
2

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
}
0

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
  • 312
  • 2
  • 9
0

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

{
  ...
  "compilerOptions": {
    ...
    "allowSyntheticDefaultImports": true,
tbo47
  • 1,115
  • 1
  • 12
  • 10