angulartypescriptwebpackangular-cliobject-hash

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:


Solution

  • After the commit 1e0835c, upgrading to v2.0.1 should solve the problem (see the author's comment in the reported issue)