aws-sdk-js: browserify not working for node apps

Trying to browserify my node code for use in lambda to help combat the cold start issues (which also raises the ceiling on the hard 50MB zipped limit).

Is browserfying for node not supported? Ultamately I’d like to just use the --node browserify option, but its not working.

Ex: (I wont be hard coding creds, this is just for ease of reproduce.)

var AWS = require('aws-sdk');
AWS.config.update({
  accessKeyId: 'mykey',
  secretAccessKey: 'mysecret'
});
var s3 = new AWS.S3();
s3.listBuckets(function(err, data) {
  console.log(err, data);
});

If I run browserify bundletests.js > bundled.js then run node bundle.js I get XMLHttpRequest is not defined which is expected since I’m not running in the browser.

So I run browserify bundletests.js --dg --no-builtins --no-commondir > bundled.js to not use the builtins and I get

module.js:340
    throw err;
          ^
Error: Cannot find module '_process'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at s (/Users/ryan/projects/JAWS/tests/bundle/testapp/myLambda/bundled.js:1:176)

Any ideas?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 20 (11 by maintainers)

Commits related to this issue

Most upvoted comments

@doapp-ryanp PR #1123 adds support for using browserify or webpack with node projects. It also lets you require individual services in your code.

Here’s an example of a quick and dirty module:

// handler.js
var S3 = require('aws-sdk/clients/s3');

var handler = function() {
    var s3 = new S3();
    s3.headObject({
        Bucket: 'BUCKET',
        Key: 'KEY'
    }, function(err, data) {
        console.log(err, data);
    });
};

module.exports = handler;

Running browserify --node --standalone='lambda' handler.js -o bundle.js created a bundle that is significantly less than the size of the full SDK (in this example, less than 700 KB). That’s without minifying the code as well. The same can also be done using webpack.

Let me know if this helps with your development! I’ll update again once this feature is in master.

@chrisradek sorry it took so long for me to get to this, but I have just tested the aws-sdk for JS against browserify using this new componentized functionality and it works AWESOME.

It is producing super small code sizes. This functionality will now allow me to stay up to date with the most recent aws-sdk-js without paying a performance penalty.

Great work!!

ps - this issue can be closed IMO

The PR was just merged and released as part of 2.6.0. We’ll work on updating docs next, but you should be able to start taking advantage of this functionality now.