简体   繁体   中英

error TS2307: Cannot find module 'crypto'

Im writting a web with Angular 6. I need to hash a string client-side, so i'm using createHash("sha256") from 'crypto' .

actually, I just wrote createHash and vscode suggest me the import, which looks like this:

import { createHash } from "crypto";

(this way of importing seems to be fine, and it's used in some typescript tutorial, here ) and then:

var hashed = createHash("sha256").update(data).digest()

all syntax is being suggested by vscode, with docstrings and everything. But at the moment of compile with npm start I get:

ERROR in src/domain/User.ts(2,28): error TS2307: Cannot find module 'crypto'.

as far as I could understand, crypto is now built-in into node, and I shouldn't have any problem importing it.

also notice that if I run node in terminal to open a REPL, entering 'crypto' gives me an output that suggest that everything works well.

Here are the versions of everything I think that cares:

node --version: v10.15.1
ng --version:
Angular CLI: 6.2.9
Node: 10.15.1
OS: linux x64
Angular: 6.1.10
typescript 2.9.2
webpack 4.16.4

Any help will be appreciated.

For Typescript 2.* and Angular 2+ -

  1. Install and add this package to the devDependencies.

     npm install @types/node --save-dev
  2. In tsconfig.app.json under the compilerOptions, add this -

     "types": [ "node" ],

You need to install the dependency. There was the same question, try to do the same, it must help How to use 'crypto' module in Angular2?

I had the same issue. The first solution I found was to add the following to packages.json

"browser": {
    "crypto": false
}

Since all I wanted was to generate a sha256 digest to use a an index I removed crypto-js and @types/crypto=js and replaced it with jshashed .

yarn add jshashes

I then modified my digest service to reflect the new library:

import { Injectable } from '@angular/core';
import * as JsHashes  from 'jshashes';

@Injectable({
  providedIn: 'root'
})

export class CryptoService {
  getSha256HEX(value: string): string {
     const hash =  new JsHashes.SHA256;
     return hash.hex(value);
  }
}

Simply add CryptoService to your module file apps.module.ts

import { CryptoService }  from '@services/crypto.service';

Note: The @services path defined in tsconfig.json

All that is left is to declaref CryptoService in the constructor were needed:

constructor( private _crypto: CryptoService; ) {}

and use it by passing a string, or if compound data (like and HTTP query string to build a cache key) stringify it.

 myHash = this._crypto.getSha256HEX(JSON.stringify(_compoundDataStruccture));

Note: jshashes supports

Digests:

  • MD5
  • SHA1
  • SHA256
  • SHA512
  • HMAC
  • RIPEND-160

Also:

  • Base64 encoding/decoding
  • CRC-32 calculation
  • UTF-8 encoding/decoding

如果您使用 typescript 为 NodeJs 制作服务器端应用程序,那么您只需要使用npm i @types/node -D安装 @types/node,然后您应该能够使用import * as crypto from "crypto"导入它

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM