Aws-sdk-js: Cannot read property 'crypto' of undefined

Created on 17 May 2017  路  9Comments  路  Source: aws/aws-sdk-js

Getting the following error when importing aws-sdk. Not sure if it is a bug or just my set-up.

Seems to be happening on the following lines:
if (_global.crypto && crypto.getRandomValues) {
whatwgRNG = function(size) {
var bytes = new Uint8Array(size);
crypto.getRandomValues(bytes);
return bytes;
}
}

third-party

Most helpful comment

@jasoneversole Can you reopen this? I stumbled on the same issue. There is an ancient version of crypto-browserify (1.0.9) included in aws. This broke my build too. ~Updating it to the latest version solved it for me, but I'm not entirely sure this is without consequences.~

Update: updating to the latest version breaks.
the curlpit is line 4 in rng.ks

  var _global = this;

In strict mode the this will be undefined.
When replaced by :

  var _global = this || window || {};

it will not error out anymore, but changing files in node_modules isn't very stable. (slight understatement there!)

All 9 comments

Never-mind, this seems to have been an issue with my webpack config.

I needed to add the following:
exclude: /node_modules/,

Thanks.

@jasoneversole Can you reopen this? I stumbled on the same issue. There is an ancient version of crypto-browserify (1.0.9) included in aws. This broke my build too. ~Updating it to the latest version solved it for me, but I'm not entirely sure this is without consequences.~

Update: updating to the latest version breaks.
the curlpit is line 4 in rng.ks

  var _global = this;

In strict mode the this will be undefined.
When replaced by :

  var _global = this || window || {};

it will not error out anymore, but changing files in node_modules isn't very stable. (slight understatement there!)

@SanderElias thanks this helped us fix the issue. We monkey-patched rng.js with a patched version (the one line you suggested) in the postinstall script.

Still having this issue today, and I'm not sure why this and #1566 are closed?

Like @timotgl we had to apply a monkey patch.

Some notes for anyone finding themselves here having to do the same thing:

  • in case it's not clear, the file you want to patch is in crypto-browserify
  • I found patch-package very useful for this.

Here is my patch:

--- a/node_modules/crypto-browserify/rng.js
+++ b/node_modules/crypto-browserify/rng.js
@@ -1,7 +1,7 @@
 // Original code adapted from Robert Kieffer.
 // details at https://github.com/broofa/node-uuid
 (function() {
-  var _global = this;
+  var _global = this || window || {};

   var mathRNG, whatwgRNG;

@ericdcobb
What version of the SDK are you using? We haven't been using crypto-browserify since version 2.178.0, and started bundling our crypto dependency in browsers since version 2.183.0.

Ref: #1880

@chrisradek I was using 2.282.1, I believe, but also usingamazon-cognito-identity-js from aws-amplify. I'll admit I'm not 100% clear on how the two projects are related, but IIUC I needed amplify because I'm doing react native, so it could be that amplify is the problem and not the aws-sdk - it's just bad googling that brought me here.

I think it should be reopened. Patching files (even ad-hoc) is a good solution but would be great if you guys can fix the real problem here.

I some how cannot find the full source for this package so as to apply this patch. Another fix will be to change the crypto-browserify to amplify-crypto-browserify. Looking at the package json I couldn't find this package

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

Was this page helpful?
0 / 5 - 0 ratings