Having trouble getting sdk working with require.js (AWS class not in scope)
See original GitHub issueI’m trying to figure out how to use the browser-based aws-sdk.js with require.js when building a web app.
If I try to build a class and include aws-sdk in the require.js define, once I try to create an instance and reference the AWS class, it says it is undefined.
For example, I have a function that checks to see if the credentials are an instance of AWS.Credentials and if not, it tries to initialize it with an STS token it retrieves via Rest. The problem is, the code dies on the if(this.credentials instanceof AWS.Credentials) saying AWS is undefined. It even dies on my simple check of the needsRefresh.
This is what my define looks like - I’ll include the ‘needsRefresh’ wrapper for an example of the sort of thing that is throwing the Undefined error:
define(['require','aws-sdk','jquery'],
function (require, AWS, $) {
// Aws helper class
function AwsHelper() { };
AwsHelper.prototype = {
credentials: null,
tokenNeedsRefresh: function () {
//////////////////////////////////////////////////////////////////
// errors out on the following line with: //
// TypeError: Cannot read property 'Credentials' of undefined //
//////////////////////////////////////////////////////////////////
if(this.credentials instanceof AWS.Credentials) {
return this.credentials.needsRefresh();
} else return true;
}
};
return AwsHelper;
}
);
I also tried the following format at the top of the file:
define(function (require) {
var AWS = require("aws-sdk"),
$ = require("jquery");
/* .. */
If I remove all onLoad references to the refresh code running, it will load and I can create an instance. But as soon as I call any function that references the AWS class, it dies.
How do I make sure that AWS class definition is still in global space once an instance is spawned?
related open thread:
unresolved Stack overflow
unanswered Require.js github
unanswered AWS community forum
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (4 by maintainers)
(the idea of require.js configuration is it’s essentially an asynchronous, lazy-loaded scheme. The libraries are supposed to be loaded before any code runs. But something in how aws-sdk.js initializes doesn’t work with require.js/commonjs setup)
I did try that also - the problem is that the sdk still isn’t loaded when the require.js code runs. I even tried adding a delay/retry scheme using a setTimeout recursive check, but for some reason, it will see AWS defined but then it’s still undefined when the function is called. I’m not sure what all require.js or the aws-sdk.js is doing so I’m not entirely sure what is going on. Most of the AWS examples show setting static-public values to set the configuration, but I can’t even seem to get new instances formed when I need them to then set the values per instance.