11

I install module:

npm install --save crypto

I import it to my component:

import { createHmac } from "crypto";

But I get error:

ERROR in -------------- (4,28): Canno t find module 'crypto'.

What am I doing wrong?

eko
  • 37,528
  • 9
  • 64
  • 91
j809809jkdljfja
  • 739
  • 1
  • 8
  • 23

4 Answers4

5

To use crypto NodeJS library with Typescript (Angular >= 2 for example) follow these steps:

  1. npm install @types/node --save-dev to install NodeJS definitions
  2. In tsconfig.ts file add the following:

    "files": [ "./node_modules/@types/node/index.d.ts" ]

  3. Import the library where you want to use it with import * as crypto from 'crypto';

Guillaume
  • 6,367
  • 2
  • 24
  • 26
  • GENIUS! This adds the built-in crypto library from node.js, which actually has the createHmac function. https://www.npmjs.com/package/crypto – Kriil Jun 01 '19 at 16:09
  • 12
    I spoke too soon. It doesn't seem to work for Angular 7.. the import doesn't work. Cannot find module 'crypto' – Kriil Jun 01 '19 at 16:27
3

You need to install the definition files for a 3rd party library like crypto. So that typescript can find the "meaning" for it.

I think the definition file is:

npm install --save-dev @types/crypto-js 

Then you can import the module like:

import * as crypto from "crypto";

If you can't find the definition file for that lib, you can write it on your own or as a workaround you can declare the module as any but typescript won't be able to auto-complete the methods.

declare var crypto: any;

and use its methods like:

crypto.createHmac..
eko
  • 37,528
  • 9
  • 64
  • 91
  • 6
    But I think [crypto-js](https://www.npmjs.com/package/crypto-js) is a different module than [crypto](https://www.npmjs.com/package/crypto). – j809809jkdljfja Apr 11 '17 at 18:45
  • @johnerfx ah thanks for the feedback, you can declare the module as any or create its definition file on your own. I'll edit my answer with an example. – eko Apr 11 '17 at 18:49
  • thanks for the answers, but I still can't make it work: ERROR TypeError: crypto.createHmac is not a function – j809809jkdljfja Apr 11 '17 at 20:05
  • @johnerfx if you are using systemjs did you include it in your imports? or your index.html? or if you are using angular-cli did you include it in your scripts? I got to go now but I will check it again in the morning. – eko Apr 11 '17 at 20:07
3

I am developing with the latest versions of Angular and 'crypto-js' seems to work fine.

Install the package and the definitions:

npm install crypto-js
npm install --save @types/crypto-js

Use it:

import { SHA256, enc } from "crypto-js";
...
login() {
...
   const hashedPass = SHA256(this.loginForm.value.password).toString(enc.Hex);
...
}
Javi
  • 345
  • 3
  • 6
0

The current tsconfig.json configuration (I'm using "typescript": "~3.5.3") includes a types compiler option that should be used in this case: In tsconfig.ts file add the following:

{
  "compilerOptions": {
    "types" : [ "node" ]
   }
}

Import the library where you want to use it with import crypto from 'crypto'

Don't use import * as crypto from 'crypto': it will import deprecated symbols/functions. (you should probably see the compiler complain about it)

Acyuta
  • 9
  • 2