# Ionic 5 with angular 12 and default browser polyfills

**URL:** <https://forum.ionicframework.com/t/ionic-5-with-angular-12-and-default-browser-polyfills/217020>\
**Category:** Ionic Framework\
**Created:** [November 6, 2021, 11:26am UTC](https://forum.ionicframework.com/t/ionic-5-with-angular-12-and-default-browser-polyfills/217020 "2021-11-06T11:26:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![juanmafont](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/juanmafont/32/129874_2.png) [@juanmafont](https://forum.ionicframework.com/u/juanmafont)\
**Post date:** [November 6, 2021, 11:26am UTC](https://forum.ionicframework.com/t/ionic-5-with-angular-12-and-default-browser-polyfills/217020/1 "2021-11-06T11:26:09Z")

</div>

Hello, I’m using ionic 5 with angular 11, and all OK, but when try new angular 12 I found with a problem. some libraries give me this error:

Error: Module not found: Error: Can’t resolve ‘crypto’ …

Reading I have found this: → [Error: Can't resolve 'crypto' in Angular 12 with Webpack 5 · Issue #908 · digitalbazaar/forge · GitHub](https://github.com/digitalbazaar/forge/issues/908#issuecomment-91418864)

This errors came because webpack 5 (used at Angular 12) removed default browser polyfills. Install crypto-browserify (or library used) and add it as a resolve.fallback to your webpack.config.

In this site, said the solution is:

Add this to webpack.config.js

```auto
 module.exports = {
     resolve: {
       fallback: {
         crypto: require.resolve('crypto-browserify')
       },
     },
   };

```

but the solution is to modify webpack.config.js and I don’t known how do it with Ionic.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [November 6, 2021, 7:10pm UTC](https://forum.ionicframework.com/t/ionic-5-with-angular-12-and-default-browser-polyfills/217020/2 "2021-11-06T19:10:38Z")

</div>

Is there a reason you can’t just use [the crypto in the browser](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API)?

---

<div class="post-metadata">

**Author:** ![juanmafont](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/juanmafont/32/129874_2.png) [@juanmafont](https://forum.ionicframework.com/u/juanmafont)\
**Post date:** [November 7, 2021, 8:25am UTC](https://forum.ionicframework.com/t/ionic-5-with-angular-12-and-default-browser-polyfills/217020/3 "2021-11-07T08:25:47Z")

</div>

Hello, rapropos. thank you for your answer.

I am not using ‘crypto’ directly in my code, it’s other third party libraries that use ‘crypto’.

Error example with lib ‘randomstring’ →

/node\_modules/randomstring/lib/randomstring.js:3:13-30 - Error: Module not found: Error: Can’t resolve ‘crypto’) .

So I want to keep my code intact if possible.

---

<div class="post-metadata">

**Author:** ![juanmafont](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/juanmafont/32/129874_2.png) [@juanmafont](https://forum.ionicframework.com/u/juanmafont)\
**Post date:** [November 7, 2021, 8:50am UTC](https://forum.ionicframework.com/t/ionic-5-with-angular-12-and-default-browser-polyfills/217020/4 "2021-11-07T08:50:39Z")

</div>

From here: [javascript - Module not found: Error: Can't resolve 'crypto' - Stack Overflow](https://stackoverflow.com/questions/54162297/module-not-found-error-cant-resolve-crypto)

I have try this two options, but not success:

1 → add this to package.json (If you don’t need crypto, I think I need it, but i will try it to see if compiled, but not success )

```auto
"devDependencies": {...},   
"browser": { "crypto": false }

```

2 → Add this to tsconfig.json: (try it with node\_modules/crypto-js and node\_modules/crypto-browserify )

```auto
"compilerOptions": {
"baseUrl": "src",
"paths": {
  .....,
  "crypto": ["node_modules/crypto-js"]
}

```

Try it also with ‘node\_modules/crypto-browserify’

but not success

---

<div class="post-metadata">

**Author:** ![juanmafont](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/juanmafont/32/129874_2.png) [@juanmafont](https://forum.ionicframework.com/u/juanmafont)\
**Post date:** [November 7, 2021, 9:31am UTC](https://forum.ionicframework.com/t/ionic-5-with-angular-12-and-default-browser-polyfills/217020/5 "2021-11-07T09:31:00Z")

</div>

I have found the solution:

I’m using Ionic Framework @ionic/angular 5.8.4 with @angular/cli 13.0.1

[[javascript - Module not found: Error: Can't resolve 'crypto' - Stack Overflow](https://stackoverflow.com/a/69040711/7486794)](Solution here)

Add below to tsconfig.json to resolve crypto warning:

```auto
"paths": {
      "crypto": [
        "node_modules/crypto-js"
      ]
    }

```

and add below to angular.json

```auto
"options": {
"allowedCommonJsDependencies": [
              "crypto-js"
            ],
...
}

```

BUT BE CAREFULL , my problem was into tsconfig.json because I have:

```auto
 "compilerOptions": {
    "baseUrl": "src",

```

So I need add “…/” to my path, then the correct modification into tsconfig.json is:

```auto
"compilerOptions": {
"baseUrl": "src",
"paths": {
  .....
  "crypto": ["../node_modules/crypto-js"]
}

```

Note **‘…/’** before ‘node\_modules/crypto-js’

Now compile without problems…

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [November 7, 2021, 11:55pm UTC](https://forum.ionicframework.com/t/ionic-5-with-angular-12-and-default-browser-polyfills/217020/6 "2021-11-07T23:55:32Z")

</div>

What are you using `randomstring` for? It should be trivial to swap that out for something that doesn’t need any crypto depedencies.

---

<div class="post-metadata">

**Author:** ![juanmafont](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/juanmafont/32/129874_2.png) [@juanmafont](https://forum.ionicframework.com/u/juanmafont)\
**Post date:** [November 8, 2021, 11:40am UTC](https://forum.ionicframework.com/t/ionic-5-with-angular-12-and-default-browser-polyfills/217020/7 "2021-11-08T11:40:40Z")

</div>

Hi, rapropos, randomstring was by speed when developing, now i am using my own function and uninstall randomstring.

But I’m still using realm-web ( [https://docs.mongodb.com/realm/web/](https://docs.mongodb.com/realm/web/) ) to access atlas service , that still need crypto-js…
