The CLI and SDK work differently, in that you must explicitly assume the role when using the SDK. The SDK doesn't automatically assume the role from the config as the CLI does.
After the role is assumed, the AWS.config must be updated with the new credentials.
This works for me:
var AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
var sts = new AWS.STS();
sts.assumeRole({
RoleArn: 'arn:aws:iam::123456789:role/Developer',
RoleSessionName: 'awssdk'
}, function(err, data) {
if (err) { // an error occurred
console.log('Cannot assume role');
console.log(err, err.stack);
} else { // successful response
AWS.config.update({
accessKeyId: data.Credentials.AccessKeyId,
secretAccessKey: data.Credentials.SecretAccessKey,
sessionToken: data.Credentials.SessionToken
});
}
});