Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'crypto' of undefined #1512

Closed
jasoneversole opened this issue May 17, 2017 · 9 comments
Closed

Cannot read property 'crypto' of undefined #1512

jasoneversole opened this issue May 17, 2017 · 9 comments
Labels
third-party This issue is related to third-party libraries or applications.

Comments

@jasoneversole
Copy link

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;
}
}

@jasoneversole
Copy link
Author

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

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

Thanks.

@SanderElias
Copy link

SanderElias commented Jun 10, 2017

@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!)

@timotgl
Copy link

timotgl commented Jan 16, 2018

@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.

@ericdcobb
Copy link

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:

patch-package
--- 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;
 

@chrisradek
Copy link
Contributor

@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

@ericdcobb
Copy link

ericdcobb commented Jul 31, 2018

@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.

@matrunchyk
Copy link

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.

@ch3ck
Copy link

ch3ck commented Sep 18, 2018

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

@srchase srchase added third-party This issue is related to third-party libraries or applications. and removed third-party labels Jan 4, 2019
@lock
Copy link

lock bot commented Sep 28, 2019

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.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
third-party This issue is related to third-party libraries or applications.
Projects
None yet
Development

No branches or pull requests

8 participants