Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 778
alete Avatar asked Dec 13 '22 12:12

alete


1 Answers

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" ],
    
like image 70
Pushpak Avatar answered Dec 21 '22 11:12

Pushpak