aws-sdk-js: Doesn't work with Webpack 2

After hours of trying to get it to work, I am now convinced that aws-sdk-js doesn’t work with Webpack 2. Using the latest version from npm install aws-sdk.

Here’s what I tried:

const S3 = require('aws-sdk/clients/s3');

const Config = {
    credentials: {
        accessKeyId: //...,
        secretAccessKey: //...,
    },
    apiVersion: '2006-03-01',
    region: //...
}

let _S3 = new S3(Config);
_S3.putObject(<data>, (err, data) => console.log('done'));

Expect to see a file uploaded to S3 and the callback invoked.

Instead this error happened:

Uncaught TypeError: regionConfig is not a function
    at features.constructor.initialize (service.js?9975******:53)
    at features.constructor.Service [as constructor] (service.js?9975******:39)
    at features.constructor (util.js?bbc0******:595)
    at new features.constructor (util.js?bbc0******:595)
    at features.constructor.Service [as constructor] (service.js?9975******:30)
    at new features.constructor (util.js?bbc0******:595)
    at Object.putFile (S3Helper.js?be3d******:53)
    at SendControlView.js?0a68**:38
    at HTMLImageElement.canvasImg.onload (DrawingBoardView.js?09cf**:477)
initialize @ service.js?9975******:53
Service @ service.js?9975******:39
features.constructor @ util.js?bbc0******:595
features.constructor @ util.js?bbc0******:595
Service @ service.js?9975******:30
features.constructor @ util.js?bbc0******:595
putFile @ S3Helper.js?be3d******:53
(anonymous) @ SendControlView.js?0a68**:38
canvasImg.onload @ DrawingBoardView.js?09cf**:477

The best I can debug it to, is that var regionConfig = require('./region_config') in lib/service.js returns Object {rules: Object, patterns: Object} instead of a function.

I’m 90% sure I’m not doing anything wrong.

I can get it to work using the “load dist version” workaround:

import 'aws-sdk/dist/aws-sdk';
const AWS = window.AWS;

But this is not ideal because the whole SDK is 1.2MB and I just need S3.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (10 by maintainers)

Most upvoted comments

@Swizec Thanks, I found out the issue you’re seeing happens due to this line in your config:

extensions: ['.json', '.js', '.less', '.css', '.handlebars'],

I’m not sure how this array is read in ( @TheLarkInn might be able to explain), but it seems order matters. By default ‘.js’ appears before ‘.json’ in this list: https://webpack.js.org/configuration/resolve/#resolve-extensions

If I take your example and switch ‘.json’ and ‘.js’, I don’t get an error anymore.

My assumption is that when webpack sees region_config is required here, it resolves it using .json instead of .js, based on how you’ve configured your extensions array. I imagine it only resovles it using .json in this case because there is an actual region_config.json file imported as well.

Are you able to update your config to see if that fixes things? In any case, I think we can still make the update in your PR since now we know what’s causing the issue and can add tests 😃

The above example did not solve this issue. 😦