Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I import from AutoMapper (TS) and generate a Require statement

I am using the AutoMapper package with TypeScript version 2.0.3 but when I try to import it, I always get the error "'automapper-ts' is not a module":

enter image description here

enter image description here

If I look in the definition file, it starts with:

declare module AutoMapperJs {

and it contains export statements.

If I change my import statement to this, it compiles:

import {} from 'automapper-ts/dist/automapper';

but then I don't get a Require statement and the global automapper static variable is not found at runtime. To get it to work, I have to replace the import statement with a reference:

/// <reference path="../../node_modules/automapper-ts/dist/automapper.d.ts" />

and then add a manual JavaScript link:

<script src="lib/automapper-ts/dist/automapper.js"></script>

Can anyone explain what I am doing wrong?

like image 901
Rob Kent Avatar asked Nov 11 '16 11:11

Rob Kent


1 Answers

Try import "automapper-ts/dist/automapper"; which should import the package and let you use the automapping stuff such as automapper.initialize(<...>); and so on.

This is similar to the RxJS package which is also not modularized and global (as far as I remember).

Thanks. Following the above suggestion and comments below, here is what I had to change to get everything working, although I still get the compiler error during build (see note below):

Add this to the systemjs.config.js file:

    map: {
        'rxjs': 'npm:rxjs',
        'automapper-ts': 'npm:automapper-ts',
    },
    packages: {
        "automapper-ts": {
            defaultExtension: 'js'
        },
    }

Then just use the import statement in my component.ts file:

import 'automapper-ts/dist/automapper';

This generated the correct Require statement.

like image 140
silentsod Avatar answered Sep 24 '22 21:09

silentsod