Sunday, 1 December 2019

Using object-hash in an Angular 8 project causes "Cannot read property 'crypto' of undefined" error

I have an Angular-CLI project which makes use of the object-hash library for creating hashes from objects. Normally I would just put

import * as objectHash from 'object-hash'

at the top of one of my component files and then do const hash = objectHash(obj) anywhere I needed a hash.

I just upgraded my project to Angular 8 and suddenly, the Angular project serves great, but when I run a production build, I get an error: Cannot read property 'crypto' of undefined.

This is because Angular 8 generates differential JavaScript, generating some bundles for newer browsers as <script type="module"> and older browsers as <script nomodule>. When a script has type="module", apparently this is treated differently- so the object-hash library's reference to this is broken: https://github.com/puleos/object-hash/issues/71

Does anyone have any insight into how I might resolve this?

Potential solutions I'm seeing are:

  • Importing the object-hash library differently somehow

  • Using a completely different browser-compatible object-hashing library (I haven't found one yet)

  • Getting Angular-CLI v8 to stop generating differential JS and just spit out old-fashioned bundles (I haven't found a solution to this one yet)
  • Downgrading to Angular 7


from Using object-hash in an Angular 8 project causes "Cannot read property 'crypto' of undefined" error

No comments:

Post a Comment