Last updated on

Angular can't use crypto


 error TS7016: Could not find a declaration file for module ‘crypto’. ‘any’ type.
  Try `npm i —save-dev @types/crypto-browserify` if it exists or add a new declaration (.d.ts) file containing `declare module ‘crypto’;`

9 import * as Crypto from ‘crypto’;

run cmd:

npm install crypto-browserify stream-browserify assert stream-http https-browserify os-browserify

change compilerOptions’s paths in tsconfig.json:

{

    “compilerOptions”: {

        “paths” : {

          “crypto”: [”./node_modules/crypto-browserify”],

          “stream”: [”./node_modules/stream-browserify”],

          “assert”: [”./node_modules/assert”],

          “http”: [”./node_modules/stream-http”],

          “https”: [”./node_modules/https-browserify”],

          “os”: [”./node_modules/os-browserify”],

        }

    }

}

add to src/polyfills.ts:

(window as any).global = window;

import { Buffer } from ‘buffer’;

global.Buffer = Buffer;

global.process = {

   env: { DEBUG: undefined },

   version: ”,

   nextTick: require(‘next-tick’)

   } as any;

and for TypeScript

run cmd:

npm install -d @types/node

add compilerOptions’s types to tsconfig.app.json:

{

    “compilerOptions”: {

        “types”: [

            ”./node_modules/@types/node”

        ]

    }

}

now,

run ng serve again!