0

I'm trying to integrate crypto-js with my angular 2 app.

Global module exports may only appear at top level angular typings.

I have read the below contents link 1

In webpack how do I fix 'import declarations may only appear at top level of a module'?

But i'm not able to implement a solution

below are the steps i followed.

1) Installed crypto-js using type --> npm install --save @types/crypto-js

2) Then in my service added reference. Below is the service file

/// <reference path="../../../typings/index.d.ts" />

import { Injectable } from '@angular/core';
import { LocalStorageService } from 'angular-2-local-storage';

import * as CryptoJS from 'crypto-js';

@Injectable()
export class AppLocalStorageService {

    constructor(public localStorageService: LocalStorageService) { }

    public setItem(key:any,value:any) {

        var enc = CryptoJS.MD5(key);
        console.log(enc);

    }

    public getItem(key:any) {
        return key;


    }

}

Still getting the error.Anything else i should do?

Thanks

Shruti Nair
  • 1,982
  • 8
  • 30
  • 57

1 Answers1

2

For my scenario it was ionic appication, i faced the same issue while using typings. After some research i found that instead of typings it is best to use @type. So i used below npm package

https://www.npmjs.com/package/@types/crypto-js

After that i imported the module as below

import * as CryptoJS from 'crypto-js';

Hope it will work for you.

Enjoy your coding :)

Linson
  • 655
  • 1
  • 7
  • 21