question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Having trouble getting sdk working with require.js (AWS class not in scope)

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
scottw-finaocommented, Dec 19, 2019

(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)

1reaction
scottw-finaocommented, Dec 19, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using aws-sdk.js with require.js in a browser - Stack Overflow
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...
Read more >
Modular packages in AWS SDK for JavaScript
One of the major changes in the JavaScript SDK v3 is modularized packages. This blog post explains why we decided to publish modular ......
Read more >
AWS SDK for JavaScript
The SDK provides first class TypeScript support and makes it easy to call AWS services using idiomatic JavaScript APIs to build Node.js, web,...
Read more >
Getting Started in Node.js - AWS SDK for JavaScript
Create a new file named sample.js to contain the example code. Begin by adding the require function calls to include the SDK for...
Read more >
Class: AWS.MarketplaceCatalog — AWS SDK for JavaScript
You can also create your own applications on top of the Catalog API to manage your products on AWS Marketplace. Sending a Request...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found