93

I get the following error, when testing some javascript code, transpiled from a typescript file.

Here is the error:

Error: _mapAction2.default is not a constructor

Here is the line of code that caused the error:

var mapAction = new MapAction(MapActionType.POLYGONDRAGGED, []);

Here is the original typescript-file map-action.ts:

import { IMapAction} from './imap-action';
import { MapActionType } from './map-action-type.enum';
import {LatLngLiteral} from 'angular2-google-maps/core';

export class MapAction implements IMapAction{
    type: MapActionType;
    paths: Array<LatLngLiteral>;

    constructor(mapActionType: MapActionType, paths: Array<LatLngLiteral>){
        this.type = mapActionType;
        this.paths = paths;
    }

    public getType(): MapActionType{
        return this.type;
    }

    public getPaths(): Array<LatLngLiteral>
    {
        return this.paths;
    }

}

Here is the transpiled .js-file map-action.js:

"use strict";
class MapAction {
    constructor(mapActionType, paths) {
        this.type = mapActionType;
        this.paths = paths;
    }
    getType() {
        return this.type;
    }
    getPaths() {
        return this.paths;
    }
}
exports.MapAction = MapAction;
//# sourceMappingURL=map-action.js.map
Mel
  • 5,837
  • 10
  • 37
  • 42
ChristopherMortensen
  • 1,093
  • 2
  • 8
  • 10

7 Answers7

106

You need to export a default value which will look like:

export default class MapAction implements IMapAction {...

And import it as:

import MapAction from './map_action_file';

Alternatively, if you want to export multiple things from the module you can do something like:

export class MapAction ...
export class MapSomethng ...

And import it as follows:

import { MapAction, MapSomething } from './map_action_file';
euvl
  • 4,716
  • 2
  • 25
  • 30
30

Another solution would be to add "esModuleInterop": true, into tsconfig.json.

esModuleInterop allows default imports from modules with no default export.

Željko Šević
  • 3,743
  • 2
  • 26
  • 23
20

Check that your import is correct. you might miss {} for example. Change

import LatLngLiteral from '';

to

import { LatLngLiteral } from '';
Datsos
  • 528
  • 1
  • 8
  • 13
  • 1
    There is a big difference between importing with brackets and without. If you don't know that difference, you need to spend 5 minutes to understand Default Exports vs Named Exports in JS – TetraDev Jul 26 '22 at 16:02
  • Of course I know the difference, but this error mostly comes when trying to import a Named Export that already exists in a file. It's way more common to change the `import` than the `export` that probably already exists in codebase. – Datsos Aug 31 '23 at 10:48
1

If you had tried the answers here and they didn't help then verify the stack trace of your error carefully - I found the circular dependency in my code (but the error was the same as in this question so the cause was absolutely not evident)

Nick
  • 144
  • 1
  • 4
-1

I faced this issue when I was using node JS and mongoDB. the issue was on the way I imported the User model.

This is the way worked for me

import { User } from '../models/User'
ncutixavier
  • 455
  • 5
  • 4
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/29883608) – Emma Sep 21 '21 at 16:00
-1

I faced this problem recently. My imports were correct. I had to compile my TypeScript service because the js-class of my ts-class had differentiated code.

I did this in IntelliJ

Joules
  • 1
  • 1
-8

Since classes in javascript are syntactic sugar, I figured I'd try to solve this issue without them. For me, switching the architecture over to prototypes seems to have solved my issue. Posting in case anyone else runs into this issue but is already doing export default

arshbot
  • 12,535
  • 14
  • 48
  • 71
  • 2
    This doesn't actually give more than the barest hint how to solve the actual question. I _think_ you're suggesting that he should rewrite the imported module to not use classes (but I could be far wrong...). While perhaps the OP _could_ do that, most people coming here with the same question are going to be dealing with third party modules that they _can't_ modify. – Auspex Aug 29 '19 at 09:11